I'm trying to automate the preparation of Server Shares but I ran into a few errors. I'm reading the information from a INI file to form a Hashtable. The variable values are referenced from the Hashtable. Here is the code:
Get-Content -Path "C:\users\`$jstrode\Configuration2.ini" |
foreach-object `
-begin {
# Create an Hashtable
$hash=@{}
} `
-process {
# Retrieve line with '=' and split them
$key = [regex]::split($_,'=')
if(($key[0].CompareTo("") -ne 0) -and ($key[0].StartsWith("[") -ne $True))
{
# Add the Key, Value into the Hashtable
$hash.Add($key[0], $key[1])
}
}
## Variables
$i = 0
$Server = ($hash.GetEnumerator() | Where-Object {$_.name -match "server$i"}).Value
$Path = ($hash.GetEnumerator() | Where-Object {$_.name -match "Path$i"}).Value
$ShareLocation = ((("\\{0}\{1}") -f $Server,$Path).replace(':','$')).replace(' ','')
$Share = ($hash.GetEnumerator() | Where-Object {$_.name -match "Share$i"}).Value
$ShareRead = ($hash.GetEnumerator() | Where-Object {$_.name -match "Read$i"}).Value
$ShareModify = ($hash.GetEnumerator() | Where-Object {$_.name -match "Modify$i"}).Value
$ShareFullControl = ($hash.GetEnumerator() | Where-Object {$_.name -match "FC$i"}).Value
$NTFS_Permissions = ("Nash\UTLPLN-NAS-PROD-RW","MODIFY","Allow");("Nash\UTLPLN-NAS-PROD-RO","READ","Allow")
#$Share = 'C_APPSTest2'
#$Path = 'C:\Test0\Test2'
## Create Folder
IF (!(test-path $path))
{
write-host "Creating folder: " $path -ForegroundColor green
New-Item -Path $path -ItemType directory
}
else
{
write-host "The folder already exists: "$path -ForegroundColor Yellow
}
## Create Share
$Path
IF (!(Get-SmbShare -Name $share -ErrorAction SilentlyContinue))
{
write-host "Creating share: " $share -ForegroundColor green
New-SmbShare –Name $share –Path $path –Description ‘Test Shared Folder’ –FullAccess 'administrators' –ReadAccess 'Everyone','Nash\USWF-DEV-ANNEX-MASNAC' -ChangeAccess 'Authenticated Users'
}
else
{
write-host "The share already exists: " $share -ForegroundColor Yellow
}
Output
Creating folder: C:\Test0
New-Item : Cannot find drive. A drive with the name ' C' does not exist.
At line:35 char:21
+ New-Item -Path $path -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ( C:String) [New-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
Creating share: C_APPS
New-SmbShare : One or more parameter values passed to the method were invalid.
At line:47 char:21
+ New-SmbShare –Name $share –Path $path –Descripti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare)
[New-SmbShare], CimException
+ FullyQualifiedErrorId : MI RESULT 4,New-SmbShare
When I put in discrete values it works like a charm.
Get-Content -Path "C:\users\`$jstrode\Configuration2.ini" |
foreach-object `
-begin {
# Create an Hashtable
$hash=@{}
} `
-process {
# Retrieve line with '=' and split them
$key = [regex]::split($_,'=')
if(($key[0].CompareTo("") -ne 0) -and ($key[0].StartsWith("[") -ne $True))
{
# Add the Key, Value into the Hashtable
$hash.Add($key[0], $key[1])
}
}
## Variables
$i = 0
$Server = ($hash.GetEnumerator() | Where-Object {$_.name -match "server$i"}).Value
$Path = ($hash.GetEnumerator() | Where-Object {$_.name -match "Path$i"}).Value
$ShareLocation = ((("\\{0}\{1}") -f $Server,$Path).replace(':','$')).replace(' ','')
$Share = ($hash.GetEnumerator() | Where-Object {$_.name -match "Share$i"}).Value
$ShareRead = ($hash.GetEnumerator() | Where-Object {$_.name -match "Read$i"}).Value
$ShareModify = ($hash.GetEnumerator() | Where-Object {$_.name -match "Modify$i"}).Value
$ShareFullControl = ($hash.GetEnumerator() | Where-Object {$_.name -match "FC$i"}).Value
$NTFS_Permissions = ("Nash\UTLPLN-NAS-PROD-RW","MODIFY","Allow");("Nash\UTLPLN-NAS-PROD-RO","READ","Allow")
#$Share = 'C_APPSTest2'
#$Path = 'C:\Test0\Test2'
## Create Folder
IF (!(test-path C:\Test0))
{
write-host "Creating folder: " C:\Test0 -ForegroundColor green
New-Item -Path C:\Test0 -ItemType directory
}
else
{
write-host "The folder already exists: "C:\Test0 -ForegroundColor Yellow
}
## Create Share
IF (!(Get-SmbShare -Name C_APPS -ErrorAction SilentlyContinue))
{
write-host "Creating share: " C_APPS -ForegroundColor green
New-SmbShare –Name C_APPS –Path C:\Test0 #–Description ‘Test Shared Folder’ –FullAccess 'administrators' –ReadAccess 'Everyone','Nash\USWF-DEV-ANNEX-MASNAC' -ChangeAccess 'Authenticated Users'
}
else
{
write-host "The share already exists: " C_APPS -ForegroundColor Yellow
}
Nash\UTLPLN-NAS-PROD-RO
READ
Allow
The folder already exists: C:\Test0
Creating share: C_APPS
Name ScopeName Path Description
---- --------- ---- -----------
C_APPS * C:\Test0
I'm puzzled why I am getting errors when the variables $Share and $Path expand to the proper values.