首页 > 解决方案 > 通过脚本运行时无法在 new-smbshare 中添加多个组

问题描述

当我运行我的脚本时,我收到以下错误。但是,当我直接在 Powershell 上运行 New-SMBShare 命令时,它会顺利通过。我尝试将 $groups 作为数组和字符串传递,但不断收到相同的错误。我尝试让它在用户名之前显示域并且仍然相同。我希望有人能帮帮忙。

请原谅我凌乱的代码,这是我第一次编写脚本,也是第一次使用 PowerShell。

代码:

<#
# Author:       Franklin Reyes
# File name:    sharedFolderCreation.ps1
# Purpose:      Facilitates shared drive creation process
# Version:      v1
# Date:         9/24/2020
#>

# Remote server where folder will be replicated **CHANGE THIS TO THE CORRECT SERVER HOSTNAME OR IT WILL FAIL**
$backupServer="WIN-R9HLL0ILD37"

# Counter for While Loop
$counter=0

echo ""
echo "*************************************************"
echo "The following drives are available:"
echo "*************************************************"

# Prints out a table of all the current drives on the system, along with the free space available and the drive's capacity.
Get-WmiObject -Class Win32_Volume | 
        Select DriveLetter,
            @{Label="FreeSpace (In GB)";Expression={[math]::Round($_.Freespace/1gb,2)}},
            @{Label="Capacity (In GB)";Expression={[math]::Round($_.Capacity/1gb,2)}},
            Label |
        Format-Table -AutoSize

# Prompts user to select the drive
$drive= Read-Host "Please select the drive you would like to use (Only enter letter and press the Return key)"

echo ""
echo "You have selected the [$drive]:\Share drive"
echo ""

# Prompts user to enter the name of the folder and stores value in the folderName variable
$folderName = Read-Host "Enter the Shared Folders name"

echo ""
$group=Read-Host "Enter the Security group to add to the Shared Folder"
$groups= "'"+$group+"'"
echo "Chosen groups are: $groups"

$path=$drive + ":\Shares\" + $folderName

# Checks to see that user input is not empty
while($groups -eq ""){
    echo ""
    echo "You did not enter a group"
    echo ""
    $group=Read-Host "Enter the Security group to add to the Shared Folder"
    $groups='$group'
    echo ""
}

# While loop allowing user to enter more than one group
while ($TRUE)
{
    $input= Read-Host "Would you like to enter another group? (Enter 'y' or 'n')"
    if ($input -eq "y"){
        $anotherGroup= Read-Host "Please enter group name"
        $groups=$groups + ",'" + $anotherGroup + "'"
        echo "Chosen groups are: $groups"
    }elseif($input -eq "n"){
        break
    }
    else{
        echo "You did not enter a valid response"
    }
}

# Checks if folder exists. If it does not, then it creates new folder under pre-specified path.
if (Test-Path $drive":\Shares\"$folderName -PathType Container){
    echo "The folder [$folderName] already exists!"
}else {
    New-Item $path -ItemType directory
    
    if (Test-Path $path -PathType Container){
    # Folder was created
        echo ""
        echo "*************************************************"
        echo "Success! The folder $folderName has been created on this server!"
        echo "*************************************************"
    
        echo ""
        echo "*************************************************"
        echo "Checking to see if the folder has been created in $backupServer"
        echo "*************************************************"
        echo ""
        
        # Checks up to 3 times to see if the folder has been created in $backupServer
        while ($counter -lt 3){
            $test=Test-Path \\$backupServer\$drive$\Shares\$folderName -PathType Container
            if($test -eq $True){
                echo ""
                echo "*************************************************"
                echo "The folder $folderName has been created on $backupServer!"
                echo "*************************************************"
                break
            }else{
                start-sleep -s 5
                $counter=$counter + 1
                echo "Trying again: Try #$counter"
            }
        }
    }else{
        echo ""
        echo "*************************************************"
        echo "Something went wrong. The folder [$folderName] was not created."
        echo "*************************************************"
    }

    <# If folder has not been created on $backupServer, the script will end and let user know that the folder was not
       created. The shares will also not be created for either local or remote server. If the folder is created, it will 
       continue to create the shares #>
    if ($test -eq $False){
        echo "File was not created on $backupServer"
    }else {         
        # Creates shared folder on this PC and gives read access to Everyone and full access to administrators
        New-SmbShare -Name $folderName"$" -Path $path -FullAccess $groups
        echo ""
        echo "*************************************************"
        echo "Creating share on [$backupServer]"
        echo "*************************************************"
        echo ""
            
        #Creates shared on backupServer
        $session=New-CimSession -ComputerName $backupServer
        New-SmbShare -Name $folderName"$" -Path $path -FullAccess $groups -CimSession $session
        Remove-CimSession -CimSession $session
    }   
}

这是带有错误的输出:

    PS C:\Users\freyes.FRANK\Desktop> .\runpowershell.bat

*************************************************
The following drives are available:
*************************************************

DriveLetter FreeSpace (In GB) Capacity (In GB) Label
----------- ----------------- ---------------- -----
                         0.02             0.05 System Reserved
C:                      26.94            49.45
                         0.08             0.49
D:                          0             0.06 VBox_GAs_6.1.10


Please select the drive you would like to use (Only enter letter and press the Return key): C

You have selected the [C]:\Share drive

Enter the Shared Folders name: testss

Enter the Security group to add to the Shared Folder: test1
Chosen groups are: 'test1'
Would you like to enter another group? (Enter 'y' or 'n'): y
Please enter group name: test2
Chosen groups are: 'test1','test2'
Would you like to enter another group? (Enter 'y' or 'n'): n


    Directory: C:\Shares


Mode          LastWriteTime Length Name
----          ------------- ------ ----
d-----  9/28/2020   1:39 PM        testss

*************************************************
Success! The folder testss has been created on this server!
*************************************************

*************************************************
Checking to see if the folder has been created in WIN-R9HLL0ILD37
*************************************************


*************************************************
The folder testss has been created on WIN-R9HLL0ILD37!
*************************************************
New-SmbShare : No mapping between account names and security IDs was done.
At C:\Users\freyes.FRANK\Desktop\sharedFolderCreation.ps1:118 char:3
+         New-SmbShare -Name $folderName"$" -Path $path -FullAccess $gr ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException
    + FullyQualifiedErrorId : Windows System Error 1332,New-SmbShare


*************************************************
Creating share on [WIN-R9HLL0ILD37]
*************************************************

New-SmbShare : No mapping between account names and security IDs was done.
At C:\Users\freyes.FRANK\Desktop\sharedFolderCreation.ps1:127 char:3
+         New-SmbShare -Name $folderName"$" -Path $path -FullAccess $gr ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException
    + FullyQualifiedErrorId : Windows System Error 1332,New-SmbShare
    + PSComputerName        : WIN-R9HLL0ILD37


标签: powershellscriptingsmb

解决方案


问题是您在事物周围强制使用单引号。如果您手动列出字符串,那么您将在字符串周围加上引号,但如果您使用的是变量,则不需要它们。试试这个:

$group=Read-Host "Enter the Security group to add to the Shared Folder"
[array]$groups= $group

# Checks to see that user input is not empty
while($groups -eq ""){
    echo ""
    echo "You did not enter a group"
    echo ""
    $group=Read-Host "Enter the Security group to add to the Shared Folder"
    [array]$groups=$group
    echo ""
}
echo "Chosen group is: $groups"

# While loop allowing user to enter more than one group
while ($TRUE)
{
    $input= Read-Host "Would you like to enter another group? (Enter 'y' or 'n')"
    if ($input -eq "y"){
        $anotherGroup= Read-Host "Please enter group name"
        $groups += $anotherGroup
        echo "Chosen groups are: $($groups -join ', ')"
    }elseif($input -eq "n"){
        break
    }
    else{
        echo "You did not enter a valid response"
    }
}

推荐阅读