2

It's been a little while since I've touched PowerShell so struggling to get this working as I expect.

$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

So I'm expecting to have it pull both the drive letter, location and username then output the computername as a text file. I've read lots of other forums but just cannot get both the Get-WmiObjects to work together.

Apologies if this is in the wrong forum was unsure which would be best.

Thanks

2 Answers 2

4

There's a couple things wrong here.

  1. $Result1 GetWmiObject I assume there's supposed to be an = here?
  2. $Result | Out-File isn't playing nice with the mix of objects in the array. Might want to For-EachObject over it instead using Add-Content.
  3. You're adding your results outside the loop, meaning you're only going to get your last $computer in the $computerlist

Might want to do something like the following:

$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" $_}
}
3
  • Perfect thanks Tanner, shame all my remote attempts are being blocked by Firewall now GRR.. Script working perfectly though.
    – CharlesH
    Jul 14, 2014 at 14:53
  • Pretty sure that won't work remotely.
    – NetMage
    Feb 10, 2017 at 23:01
  • @NetMage Sure it does. Just need to configure remote WMI: msdn.microsoft.com/en-us/library/ee309377(v=vs.85).aspx
    – rtf
    Feb 10, 2017 at 23:03
-1

To get around powershell remote scripting, there are 2 ways i've found.

  1. If you create a batch file with the following commands, this will run on a remote computer.

powershell.exe -executionpolicy bypass -file "FILE PATH HERE"

  1. Run your script in Powershell ISE. Select all of your script and click on the run selection button.
1
  • Your answer does not answer the author's question, but is an answer, on how to get around a group policy that prevents Powershell scripts from being executed ( i.e. Restricted from Get-executionpolicy). This also likely won't work on computer connected to a active domain.
    – Ramhound
    Nov 29, 2016 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .