powershell Get-WmiObject Win32_MappedLogicalDisk dont return remote computer

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

Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
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

0
aikimarkCommented:
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

0
Kelly GarciaSenior Systems AdministratorAuthor Commented:
it is nothing to do with permissions, if I do get-wmiobject win32_computersystem -computername computername it works
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.