大家好
我想在我的Powershell脚本中弄清楚invoke-Command和Enter-PSsession的情况。我同时运行相同的代码,但在我的Invoke-Command中,我收到一条错误消息。
此代码是一个更大的控制器,我们使用我们的第一行支持在我们的文件服务器上创建共享。 该位作为控制器的一部分写入,它是原始控制器的直接副本。顶部变量仅供脚本使用,并设置为说明问题并对其进行故障排除。
我的研究导致,我的问题是最后3行,如下所示,因为它在Invoke-Command中运行。我不明白为什么它,在Invoke-command中没有工作,因为它在PSsession中工作正常。
我想这是第二次通过Credential的事情,但是它也应该在PSsession中出错。如果这不正确,请解释原因: - )
$ACL = New-Object System.Security.AccessControl.FileSystemAccessRule($($usergroup.SamAccountName), "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
$RootACL.AddAccessRule($ACL)
Set-Acl -Path $Path$sharename -AclObject $RootACL -ErrorAction 'Stop'
在我输入PSsession(使用Enter-PSsession -computername Server 1 -Credential $ Credential)后,此代码正在运行。
$Credential = (Get-Credential -Credential user1)
## For test only - Below ##
$PSComputerName = "server1"
$Drive = "e:\"
$DriveLetter = ($Drive -split ":")[0]
$Usergroup = Import-Clixml -Path "C:\UserGroup.xml"
$Path = "\\$PSComputerName\$DriveLetter$\"
$Sharename = "User1test" #$textbox1.Text
$users = "user1","User2"
## For test only - Above ##
if (-not (Test-Path -Path ($Drive + $Sharename)))
{
New-Item -ItemType directory -Path ($Drive + $Sharename) -ErrorAction 'Stop' | Out-Null
$RootACL = Get-Acl -Path ($Drive + $Sharename) -ErrorAction 'Stop'
$RootACL.SetAccessRuleProtection($false, $true)
}
Else
{
$RootACL = Get-Acl -Path ($Drive + $Sharename) -ErrorAction 'Stop'
$RootACL.SetAccessRuleProtection($false, $true)
}
$ACL = New-Object System.Security.AccessControl.FileSystemAccessRule($($usergroup.SamAccountName), "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
$RootACL.AddAccessRule($ACL)
Set-Acl -Path $Path$sharenavn -AclObject $RootACL -ErrorAction 'Stop'
$Credential = (Get-Credential -Credential user1)
$PSComputerName = "server1"
$Drive = "e:\"
$DriveLetter = ($Drive -split ":")[0]
$Usergroup = Import-Clixml -Path "I:\9514 - Drift\Powershell Scripts\Project\Oprydning af Shares og AD grupper\CreateFileShare\UserGroup.xml"
$Path = "\\$PSComputerName\($DriveLetter)$\"
$sharename = "User1test" #$textbox1.Text
$users = "user1","user2"
Invoke-Command -ScriptBlock {
param (
[String]$Sharename,
[String]$Drive
)
if (-not (Test-Path -Path ($Drive + $Sharename)))
{
New-Item -ItemType directory -Path ($Drive + $Sharename) -ErrorAction 'Stop' | Out-Null
$RootACL = Get-Acl -Path ($Drive + $Sharename) -ErrorAction 'Stop'
$RootACL.SetAccessRuleProtection($false, $true)
}
Else
{
$RootACL = Get-Acl -Path ($Drive + $Sharename) -ErrorAction 'Stop'
$RootACL.SetAccessRuleProtection($false, $true)
}
$ACL = New-Object System.Security.AccessControl.FileSystemAccessRule($($usergroup.SamAccountName), "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
$RootACL.AddAccessRule($ACL)
Set-Acl -Path $Path$sharename -AclObject $RootACL -ErrorAction 'Stop'
} -ComputerName $PSComputerName -ArgumentList $sharename,$Drive,$Usergroup -Credential:$Credential -ErrorAction 'Stop'
我收到以下错误:
Exception calling ".ctor" with "5" argument(s): "Value cannot be null.
Parameter name: identity"
At C:\Script.ps1:11 char:1
+ Invoke-Command -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
+ PSComputerName : Server1
答案 0 :(得分:1)
这是违规行
$ACL = New-Object System.Security.AccessControl.FileSystemAccessRule($($usergroup.SamAccountName), "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
似乎$($ usergroup.SamAccountName)包含null。
这是因为,在ArgumentList中发送参数时,您无法使用scriptblock之外的变量名称。可以通过将新变量赋值给参数的值并使用它来访问参数。或者,您可以直接使用$ Args []语法。
$usergroup = $Args[2] # Third argument
与其他论点类似。
您还设置了两个参数作为参数,但不是第三个参数。如果您添加了第三个参数($ usergroup),它可能会有效。但我建议更改您的代码如下,使其工作。这使得参数在Invoke-Command中被传递到-ArgumentList时非常明显。
$Credential = (Get-Credential -Credential user1)
$PSComputerName = "server1"
$Drive = "e:\"
$DriveLetter = ($Drive -split ":")[0]
$Usergroup = Import-Clixml -Path "I:\9514 - Drift\Powershell Scripts\Project\Oprydning af Shares og AD grupper\CreateFileShare\UserGroup.xml"
$Path = "\\$PSComputerName\($DriveLetter)$\"
$sharename = "User1test" #$textbox1.Text
$users = "user1","user2"
Invoke-Command -ScriptBlock {
# Assign variables from Arguments
$Sharename = $Args[0]
$Drive = $Args[1]
$Usergroup = $Args[2]
if (-not (Test-Path -Path ($Drive + $Sharename)))
{
New-Item -ItemType directory -Path ($Drive + $Sharename) -ErrorAction 'Stop' | Out-Null
$RootACL = Get-Acl -Path ($Drive + $Sharename) -ErrorAction 'Stop'
$RootACL.SetAccessRuleProtection($false, $true)
}
Else
{
$RootACL = Get-Acl -Path ($Drive + $Sharename) -ErrorAction 'Stop'
$RootACL.SetAccessRuleProtection($false, $true)
}
$ACL = New-Object System.Security.AccessControl.FileSystemAccessRule($($usergroup.SamAccountName), "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")
$RootACL.AddAccessRule($ACL)
Set-Acl -Path $Path$sharename -AclObject $RootACL -ErrorAction 'Stop'
} -ComputerName $PSComputerName -ArgumentList $sharename,$Drive,$Usergroup -Credential:$Credential -ErrorAction 'Stop'