【发布时间】: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