System Center Configuration Manager - PowerShell 远程处理

System Center Configuration Manager - PowerShell Remoting

我有一个主 SCCM 服务器 - "ABC"

后来我在另一台机器上安装了 SCCM 控制台和 PowerShell 模块 - "XYZ"

我 运行 在服务器的脚本下 - "OPQ" 并尝试远程 "XYZ"(我最近在其上安装了 SCCM 控制台)

脚本::

$Session = New-PSSession -ComputerName "XYZ" -Authentication Kerberos -Credential $Cred -ConfigurationName Microsoft.PowerShell32

Invoke-Command -Session $Session -ScriptBlock {
Import-module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"

    Set-Location PS1:\
}

错误::

访问被拒绝。 (HRESULT 异常:0x80070005 (E_ACCESSDENIED)) + CategoryInfo:OpenError:(PS1:PSDriveInfo)[导入模块],UnauthorizedAccessException + FullyQualifiedErrorId : Drive,Microsoft.PowerShell.Commands.ImportModuleCommand + PSComputerName : XYZ

找不到驱动器。名称为“”的驱动器不存在。 + CategoryInfo:ObjectNotFound:(PS1:字符串)[设置位置],DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand + PSComputerName : XYZ

看来您遇到了权限问题。以下是我如何通过我的 PSS 在我的 SCCM 环境中执行远程命令:

 $device = Invoke-Command -Session $sess -ScriptBlock {
 Import-Module (Join-Path (Split-Path $env:SMS_ADMIN_UI_PATH) 
 ConfigurationManager.psd1)
 Push-Location -Path ((Get-WmiObject -Namespace "root\SMS" -Class 
 "SMS_ProviderLocation" | Select-Object -ExpandProperty SiteCode) + ":")
 Get-CMDevice -Name $env:COMPUTERNAME
 Pop-Location
 }
 $device
 RunspaceId                        : cbc7e008-d92c-4ba3-94a3-b75f8005be98
 SmsProviderObjectPath             : SMS_CM_RES_COLL_SMS00001.ResourceID=16777221
 AADDeviceID                       : 00000000-0000-0000-0000-000000000000
 AADTenantID                       : 00000000-0000-0000-0000-000000000000
 ActivationLockBypassState         :
 ActivationLockState               :
 ADLastLogonTime                   : 3/31/2020 11:23:38 PM
 ADSiteName                        : XXXX-XX
 ...

请注意,如果您不远程处理 PSS,则需要在 Get-WmiObject 命令中指定 PSS,例如:

 (Get-WmiObject -ComputerName [YOUR PSS] -Namespace "root\SMS" -Class "SMS_ProviderLocation" | Select-Object -ExpandProperty SiteCode) + ":"

我能够通过将凭据保存在 XYZ 服务器上然后在我的 INvoke-Command 下调用它们来解决这个问题。

Like This  : 

$Session = New-PSSession -ComputerName "XYZ" Invoke-Command -Session $Session -ScriptBlock { $password = Get-Content -Path D:\Creds\creds.txt | ConvertTo-SecureString $Cred = New-Object System.Management.Automation.PSCredential ("domain\UserId", $password) Then the rest of the code. ... .. . . . }