列出映射驱动器和用户名 Powershell 脚本

列出映射驱动器和用户名 Powershell 脚本

我已经有一段时间没有接触过 PowerShell 了,因此很努力地让它按我期望的方式工作。

$computerlist = Get-Content H:\MappedDrive\ListOfMachines.txt

ForEach ($computer in $computerlist)
{

   $Result1 Get-WmiObject Win32_MappedLogicalDisk -computerName $computer | Select Name,ProviderName 
   $Result2 get-wmiobject win32_computersystem -computer $computer | select username 

}

$Result += $Result1, $Result2 | Out-File H:\MappedDrive\$computer.txt

因此我希望它能提取驱动器号、位置和用户名,然后将计算机名称输出为文本文件。我读过很多其他论坛,但就是无法让 Get-WmiObjects 一起工作。

如果这是在错误的论坛,我不确定哪个是最好的,请原谅。

谢谢

答案1

这里有几个错误。

  1. $Result1 GetWmiObject我认为这里应该有一个=
  2. $Result | Out-File无法很好地处理数组中的混合对象。可能需要For-EachObject使用 Add-Content 覆盖它。
  3. 你在循环外添加了结果,这意味着你只能$computer$computerlist

可能需要做如下的事情:

$computerlist = Get-Content H:\MappedDrive\ListOfMachines.txt

ForEach ($computer in $computerlist)
{

   $Result1 = Get-WmiObject Win32_MappedLogicalDisk -computerName $computer | Select Name,ProviderName 
   $Result2 = Get-wmiobject Win32_computersystem -computer $computer | select username 
   $Result += $Result1, $Result2 | %{Add-Content "H:\MappedDrive\$computer.txt" $_}
}

答案2

为了绕过 powershell 远程脚本,我发现了两种方法。

  1. 如果您使用以下命令创建批处理文件,它将在远程计算机上运行。

powershell.exe -executionpolicy 绕过 -file “此处为文件路径”

  1. 在 Powershell ISE 中运行您的脚本。选择所有脚本并单击运行选择按钮。

相关内容