当我手动输入命令时,Powershell脚本运行良好,但在使用PDQ Deploy部署时失败
我正在制作一个脚本来收集域上所有映射驱动器的名称和路径。不幸的是,由于WMI权限,我不能简单地将它们拉过网络,因为我在运行脚本时遇到WMI访问被拒绝错误。因此,我认为应该将脚本部署到每台计算机上本地运行,并让它们将结果写入所有用户都可以访问的共享位置 当我在PowerShell中手动键入命令时,脚本工作正常,但当我尝试通过PDQ Deploy部署它时,会生成结果文件,但其大小为0 KB。当通过PDQ运行时,是什么导致它失败?我还注意到,当我以自己的身份运行它时,它只能在本地工作,如果我以本地管理员或域管理员的身份运行它,它也会以同样的方式失败。也许我需要让它作为本地用户运行?有什么办法吗当我手动输入命令时,Powershell脚本运行良好,但在使用PDQ Deploy部署时失败,powershell,deployment,Powershell,Deployment,我正在制作一个脚本来收集域上所有映射驱动器的名称和路径。不幸的是,由于WMI权限,我不能简单地将它们拉过网络,因为我在运行脚本时遇到WMI访问被拒绝错误。因此,我认为应该将脚本部署到每台计算机上本地运行,并让它们将结果写入所有用户都可以访问的共享位置 当我在PowerShell中手动键入命令时,脚本工作正常,但当我尝试通过PDQ Deploy部署它时,会生成结果文件,但其大小为0 KB。当通过PDQ运行时,是什么导致它失败?我还注意到,当我以自己的身份运行它时,它只能在本地工作,如果我以本地管理
$computername = hostname
Get-WmiObject Win32_MappedLogicalDisk -ComputerName $computername | Out-File -Append \\servername\results\results.txt
正如Bill Stewart在评论中提到的,映射驱动器是按用户分配的。这些值存储在每个用户的注册表中。这是一个通过PSRemoting从每个用户的
HKCU:\Network
和HKCU:\Volatile Environment
(从AD主共享映射的驱动器)提取信息的函数
function Get-MappedDrive {
[CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = 'Low')]
param (
[Parameter(Mandatory = $True,
ValueFromPipelineByPropertyName = $True,
Position = 0)]
[string[]]$ComputerName
)
begin {}
process {
if ($pscmdlet.ShouldProcess($ComputerName)) {
Invoke-Command -ComputerName $ComputerName {
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null
Get-ChildItem HKU:\ |
ForEach-Object {Get-ChildItem "$($_.pspath)\Network" -ErrorAction SilentlyContinue} |
ForEach-Object {
[PSCustomObject]@{
User = (New-Object System.Security.Principal.SecurityIdentifier ($_.name -replace 'HKEY_USERS\\(.*?)\\.*','$1')).Translate( [System.Security.Principal.NTAccount]).Value
Drive = "$((Split-Path $_.name -Leaf).ToUpper()):"
Path = Get-ItemProperty -Path $_.PSPath -Name RemotePath | Select-Object -ExpandProperty RemotePath
}
}
Get-ChildItem HKU:\ |
ForEach-Object {
if (Get-ItemProperty -Path "$($_.PSPath)\Volatile Environment" -name HOMEDRIVE -ErrorAction SilentlyContinue) {
[PSCustomObject]@{
User = (New-Object System.Security.Principal.SecurityIdentifier ($_.name -replace 'HKEY_USERS.(.*?)(\\.*|$)','$1')).Translate( [System.Security.Principal.NTAccount]).Value
Drive = Get-ItemProperty -Path "$($_.PSPath)\Volatile Environment" -name HOMEDRIVE | Select-Object -ExpandProperty HOMEDRIVE
Path = Get-ItemProperty -Path "$($_.PSPath)\Volatile Environment" -name HOMESHARE | Select-Object -ExpandProperty HOMESHARE
}
}
}
Remove-PSDrive HKU | Out-Null
}
}
}
end {}
}
至于PDQ部署脚本失败的原因,是PowerShell脚本作为帐户运行,而没有文件共享权限。您可以使用
net use\\server\folder username password
或(新对象-ComObject WScript.Network)。MapNetworkDrive('Z:','\\server\folder',$false,'username','password')
来映射具有不同凭据的共享。映射的驱动器是每个用户的,而不是每台计算机的。