通过脚本运行时无法在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,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],New-SmbShare
    + PSComputerName        : WIN-R9HLL0ILD37


解决方法

问题是您要在事物周围使用单引号。如果您手动列出字符串,则可以将引号括在字符串中,但是如果您使用的是变量,则不需要它们。尝试以下方法:

$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"
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...