powershell Get-WmiObject Win32_MappedLogicalDisk dont return remote computer

Kelly Garcia
Kelly Garcia used Ask the Experts™
on
Hi All,

I have written the script below which took me while, however now I have noticed it don't return anything on remote computers


Get-WmiObject Win32_computersystem -ComputerName remotecomputer


is there a way round it, please help!

$computers = "W10000601", "w104614", "w111501", "w106061"


foreach ($computer in $Computers)

{

if(Test-Connection -ComputerName $Computer -Count 1 -Quiet) {


if ($a=Get-WmiObject Win32_MappedLogicalDisk -computername $computer | ? {$_.providername -like "\\salbefile01\groups"})

{

write-host "working on computer $computer" -ForegroundColor Green

foreach ($b in $a) {

$driveletter = $b.DeviceID

echo "driveletter = $driveletter" 

$providername = $b | select -expandproperty providername

$path = $providername

$path=$path -replace "groups", "Groups2"

$path=$path -replace 'salbefile01', 'sreadvfile02'

echo "Newpath = $path"

net use $driveletter /delete

net use $driveletter $path



}# end foreach

} # end if $a 



} #end if connection

} #end foreeach

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Get-WmiObject
By default, the current account must be a member of the Administrators group on the remote computer.
ergo you have to add -credential
$securepassword = ConvertTo-SecureString -string 'YOURSUPERSECRETPASSWORD' -AsPlainText -Force
$credential = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList ('ADMINISTRATORACCOUNT', $securepassword)
Get-WmiObject Win32_computersystem -ComputerName Bravo
Get-WmiObject Win32_computersystem -ComputerName bravo -credential $credential

Open in new window


RESULTS
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:3 char:1
+ Get-WmiObject Win32_computersystem -ComputerName Bravo
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Domain              : techsupport4me.ca
Manufacturer        : HP
Model               : ProLiant DL380 G6
Name                : BRAVO
PrimaryOwnerName    : 
TotalPhysicalMemory : 77298647040

Open in new window

Top Expert 2014

Commented:
Nothing looks out of the ordinary.  I've formatted your script for readability.
$computers = "W10000601", "w104614", "w111501", "w106061"

foreach ($computer in $Computers) {

    if(Test-Connection -ComputerName $Computer -Count 1 -Quiet) {

        if ($a=Get-WmiObject Win32_MappedLogicalDisk -computername $computer | ? {$_.providername -like "\\salbefile01\groups"}) {
    
            write-host "working on computer $computer" -ForegroundColor Green

            foreach ($b in $a) {

                $driveletter = $b.DeviceID
                echo "driveletter = $driveletter" 
                $providername = $b | select -expandproperty providername
                $path = $providername
                $path=$path -replace "groups", "Groups2"
                $path=$path -replace 'salbefile01', 'sreadvfile02'
                echo "Newpath = $path"
                net use $driveletter /delete
                net use $driveletter $path

                }# end foreach

            } # end if $a 

        } #end if connection

    } #end foreeach

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
it is nothing to do with permissions, if I do get-wmiobject win32_computersystem -computername computername it works
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
You are using -like the wrong way.  You either have to use direct comparison, e.g. with -eq for an exact match, or use wildcards. I assume you are looking for exact matches, then line 7 in aikimark's reformatted code should be:
        if ($a=Get-WmiObject Win32_MappedLogicalDisk -computername $computer | ? {$_.providername -eq "\\salbefile01\groups"}) {

Open in new window

else
        if ($a=Get-WmiObject Win32_MappedLogicalDisk -computername $computer | ? {$_.providername -like "\\salbefile01\groups*"}) {

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial