Check network drive connection
-
יום רביעי 14 מרץ 2012 10:57
Hi all,
I need a power shell script to programmatically check if a given workstation has mapped drives using a certain user. I know that mapped network drive settings are stored into HKCU\Network\{mapped drive letter} But I don't know if the mughty power shell offers some other way to accomplish this task.
כל התגובות
-
יום רביעי 14 מרץ 2012 11:06Get-ItemProperty "HKCU:\Network\*"
-
יום רביעי 14 מרץ 2012 11:28I need to check a remote computer
-
יום רביעי 14 מרץ 2012 11:40
I need to check a remote computer
Is computer in a domain and is a logged a domain user? -
יום רביעי 14 מרץ 2012 13:17yes is a computer member of a domain with a domain user logged on
-
יום רביעי 14 מרץ 2012 14:55
$pc = "pc" $user = Get-WmiObject Win32_ComputerSystem -ComputerName $pc $sid = ([Security.Principal.NTAccount]$user.UserName).Translate([Security.Principal.SecurityIdentifier]).Value $reg = [wmiclass]"\\$pc\root\default:stdregprov" $reg.EnumKey(2147483651,"$sid\Network").sNames
-
יום רביעי 14 מרץ 2012 16:51
I'm trying to build a powershell script that enumerate mapped network drives on a remote machine. I've to browse the remote registry HKEY_USERS and, if exists, I've to loop into the "Network" subkey here is my script:
$srv='pc_to_scan'
$type = [Microsoft.Win32.RegistryHive]::Users
$regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $Srv)
$key = "Network"
foreach ($valueName in $regKey.GetSubKeyNames())
{
$regKey2 = $regKey.OpenSubKey($key)
foreach ($valueName2 in $regKey2.GetSubKeyNames())
{
echo $valueName2
}
}
but the script fails telling:
You cannot call a method on a null-valued expression.
At line:9 char:52
+ foreach ($valueName2 in $regKey2.GetSubKeyNames <<<< ())
+ CategoryInfo : InvalidOperation: (GetSubKeyNames:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull- מוזג על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום רביעי 21 מרץ 2012 06:48
-
יום רביעי 14 מרץ 2012 17:03gwmi win32_logicaldisk -Filter "drivetype='4'"tack on the -computername and it works remotely
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- סומן כתשובה על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום רביעי 21 מרץ 2012 06:46
- סימון כתשובה בוטל על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום רביעי 21 מרץ 2012 06:48
-
יום רביעי 14 מרץ 2012 17:58
Andrea,
The following will work if Powershell Remoting is enabled on the remote machines ( Enable-PSRemoting )
Function Get-MapDriveUsername { Param( $computer = $env:COMPUTERNAME, $reg = "hkcu:\network", $username = "Contoso\SP_Admin" ) $computer | %{ $drives = ICM -ComputerName $_ { Param($reg) dir $reg } -ArgumentList $reg $comp = $_ $obj = New-Object psobject if($drives -ne $null) { $drives | %{ $driveletter = $_.name.split("\")[-1] foreach($i in $driveletter) { $drive = (Get-ItemProperty (Join-Path $reg $i)) if($drive.username -like $username) { $obj | Add-Member Noteproperty Computer $comp $obj | Add-Member Noteproperty Drive $drive.PSChildName $obj | Add-Member NoteProperty Path $drive.RemotePath } } } } } Write-Output $obj }
Joe -
יום רביעי 14 מרץ 2012 21:02
Andrea,
The following will work if Powershell Remoting is enabled on the remote machines ( Enable-PSRemoting )
Function Get-MapDriveUsername { Param( $computer = $env:COMPUTERNAME, $reg = "hkcu:\network", $username = "Contoso\SP_Admin" ) $computer | %{ $drives = ICM -ComputerName $_ { Param($reg) dir $reg } -ArgumentList $reg $comp = $_ $obj = New-Object psobject if($drives -ne $null) { $drives | %{ $driveletter = $_.name.split("\")[-1] foreach($i in $driveletter) { $drive = (Get-ItemProperty (Join-Path $reg $i)) if($drive.username -like $username) { $obj | Add-Member Noteproperty Computer $comp $obj | Add-Member Noteproperty Drive $drive.PSChildName $obj | Add-Member NoteProperty Path $drive.RemotePath } } } } } Write-Output $obj }
Joe
This script will not work,because when will run by another credential. -
יום חמישי 15 מרץ 2012 01:26
Kazun,
Not sure I understand....what other credentials are needed?
Joe
-
יום חמישי 15 מרץ 2012 01:54
Following command should work for you:
Gwmi Win32_LogicalDisk -filter "DriveType = 4" -Comp $strComputer
Hope this helps...!!!Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you. -
יום חמישי 15 מרץ 2012 04:53
Hey,
This seems to be duplicate thread, if I understood correctly. Check this out:
Thanks & Regards
Bhavik Solanki
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you. -
יום חמישי 15 מרץ 2012 07:30מנחה דיון
Hi,
It seems like that if we mapped a drive to the computer, then we could achieve all mapped drives by Gwmi -computername.
But if we map drives to users, them we should find out those drives based on reading the key: HKCU:\Network\*
I would like suggest you refer to the below link to use Remote Registry PowerShell Module:
In addition, here is a VBscript for you, we could also use it with powershell:
http://gallery.technet.microsoft.com/scriptcenter/3dd6af3e-edfa-4581-bc35-805314f26bb8
Hope this helps.
Best Regards,
Yan Li
Yan Li
TechNet Community Support
- סומן כתשובה על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום שני 19 מרץ 2012 01:59