【问题标题】:Invoke-Command and Enter-PSsession - DiffrencesInvoke-Command 和 Enter-PSsession - 区别
【发布时间】:2015-11-11 12:08:05
【问题描述】:

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

大家好

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

代码的设计目的是什么:

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

我的研究表明,我的问题在于最后 3 行,如下所示,因为它在 Invoke-Command 中运行。我不明白为什么,在调用命令中没有工作,因为它在 PSsession 中工作正常。

我猜这是第二次传递凭据的问题,但它也应该在 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-Pssesscion 代码:(工作中)

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

【问题讨论】:

  • 你的参数似乎比参数多。

标签: powershell powershell-remoting


【解决方案1】:

这是违规行

   $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:\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'

【讨论】:

  • 感谢您抽出宝贵时间帮助我。我已经忘记了所有关于 $Args 的事情。设置后,它解决了我的问题,我的脚本现在可以在我的 Incoke-Command 中运行。
猜你喜欢
  • 2018-07-09
  • 2019-11-01
  • 2013-01-13
  • 2014-04-18
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多