Invoke-Command 和 Enter-PSsession - 区别

Invoke-Command and Enter-PSsession - Diffrences

这里Invoke-command和Enter-PSsession有什么区别?

大家好

我试图弄清楚我的 Powershell 脚本中的 invoke-Command 和 Enter-PSsession 发生了什么。我 运行 两个都喜欢相同的代码,但在我的 Invoke-Command 中我收到一条错误消息。

代码的用途是什么:

这段代码是一个更大的控制器的一部分,我们用它来支持我们的第一行支持在我们的文件服务器上创建共享。 该位作为控制器的一部分写入,它是原始控制器的直接副本。顶部变量仅供脚本运行,设置用于说明问题并解决问题。

我的研究表明,我的问题出在最后 3 行,如下所示,因为它在 Invoke-Command 中 运行s。我不明白为什么,在 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'

我的 Enter-Pssession 代码:(有效)

我在远程服务器上输入 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:14 - 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

这是违规行

   $ACL = New-Object System.Security.AccessControl.FileSystemAccessRule($($usergroup.SamAccountName), "Modify", "ContainerInherit,ObjectInherit", "None", "Allow")

$($usergroup.SamAccountName) 似乎包含 null。

这是因为,在ArgumentList中发送参数时,不能使用脚本块外的变量名。可以通过将新变量分配给参数的值并使用它来访问参数。或者,您可以直接使用 $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:14 - 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'