I working on this script that gets networked printers from a remote computers. I am trying to to add some logic where if the computer and or username are not correct the script ends. simple right! The test-connection works but the Test-path can't get it to work. Any help will be appreciated. I plan on making this a funcation but need to get this logic together before I move on.
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
$ComputerName = Read-Host "Enter Computer Name"
$Username= Read-Host "Enter Username"
$ErrorActionPreference="SilentlyContinue"
$objUser = New-Object System.Security.Principal.NTAccount($Username)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$key = "$strSid\software\microsoft\Windows NT\CurrentVersion\Devices"
$type = [Microsoft.Win32.RegistryHive]::USERS
$regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $ComputerName)
$regKey = $regKey.OpenSubKey($key)
if(Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue ){
Write-host "Computer is online...script will continue " -ForegroundColor green
}
if (Test-path "HKU:\"){
Write-Host "Username is correct...script will contine " -ForegroundColor green
}
else {
Write-Host " invaild Username/computername....Script will end" -ForegroundColor red
exit
}
Write-Host "Values"
Write-Host "------"
Foreach($val in $regKey.GetValueNames()){$val}