forked from alexinslc/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-SMBShares.ps1
78 lines (63 loc) · 2.43 KB
/
Set-SMBShares.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<#
.SYNOPSIS
Adds the necessary SMB Shares on HyperV Hosts to allow VMs to live on UNC Shares
.DESCRIPTION
This script takes an array of HyperV hostnames and adds the necessary SMB share to each HyperV Server and allows all other HyperV servers to access all the Shares
.PARAMETER domainName
Required, String: The domain name: "MyDomain.local"
.PARAMETER hvServers
Required, Array of Strings: The list of Hyper-V Servers to add the SMB shares to: @("HVServer01","HVServer02","HVServer03")
.PARAMETER hvAdmin
Required, String: Name of the Domain account that should have full rights to the shares.
.EXAMPLE
Set-SMBShares -domainName "MyDomain.local" -hvServers @("HVServer01","HVServer02","HVServer03") -hvAdmin "HVAdmin"
.NOTES
Author: Jordan Gillespie
Date: 5/14/2014
#>
function Set-SMBShares() {
param(
[Parameter(mandatory=$true)][string]$domainName,
[Parameter(mandatory=$true)]$hvServers,
[Parameter(mandatory=$true)][string]$hvAdmin
)
try{
#Validate that all servers are in Active Directory
$errTag = $false
Foreach ($server in $hvServers)
{
$error.Clear()
$trash = Get-ADComputer $server
If ($error.length -ge 1)
{
Write-Warning "Server $server is not found in Active Directory"
$errTag = $true
}
}
If ($errTag) {Throw "Errors detected. Run aborted."}
$accessList = $domainName + "\" + $hvAdmin
Foreach ($hvServer in $hvServers)
{
$accessList += ', ' + $domainName + '\' + $hvServer + '$'
}
Foreach ($hvServer in $hvServers)
{
$error.Clear()
$path = '\\' + $hvServer + '\e$\HyperV\'
if (!(Test-Path -Path $path)) { New-Item $path -ItemType Directory }
Invoke-Command -ComputerName $hvServer -ScriptBlock ([scriptblock]::Create("New-SMBShare -Name HyperV -Path E:\HyperV -FullAccess $accessList"))
Invoke-Command -ComputerName $hvServer -ScriptBlock { 'Set-SmbPathAcl -Name HyperV' }
If ($error.length -ge 1)
{
Write-Warning "Failed to create share or set permissions on share on $server"
$errTag = $true
}
}
If ($errTag) {Throw "Errors detected. Run aborted."}
Return $true
}
catch{
Write-Host -ForegroundColor Red $_
Return $null
}
}