Powershell Mount-VHD weird behavior when issued remotely - powershell

If i run the below code on the hyper-v hypervisor (locally), it runs successfully and i get the correct Drive-Letter.
$DriveLetter = (Mount-VHD "F:\Template\win2016Template.vhdx" -PassThru |
Get-Disk | Get-Partition | Get-Volume).DriveLetter
output: G
but if i modify it to run from any normal domain joined workstation using domain admin privileges i got incorrect Drive-Letter
$DriveLetter = (Mount-VHD -ComputerName RUF-HV01 "F:\Template\win2016Template.vhdx" -PassThru | Get-
Disk | Get-Partition | Get-Volume).DriveLetter
output: T
I have no idea where "T" is coming from. The correct drive letter is G not T.
Any ideas?
Thanks,

Related

Take a disk offline from powershell

normally to take a disk offline I use the windows disk partition manager. now i would need to do it with a power shell using the drive letter.
I tried with the command:
Set-Partition -DriveLetter E -IsOffline $True
i get this error from powershell:
Set-Partition : The volume still has access path to it.
Any suggestions?
I solved it like this:
set-disk (get-partition -DriveLetter E | get-disk | select number -ExpandProperty number) -isOffline $true

Removing printer from Devices and Printers with Powershell

I am trying to make a Powershell script that will run in the OU so when the user shuts down their laptop it will remove the local printer and when they turn on their laptop it will re-add and rename the locally attached printer (this will allow the laptops to be docked into different rooms and still use the local printers). The startup script works fine. it re-adds the printer, renames it and sets the trays etc. however the shutdown script doesn't seem to be working correctly.
When the shutdown script is run it removes the printer from device manager/print manager/registry but it still seems to be showing in devices and printer as "driver not available". So when the laptop is started back up the startup script doesn't work as the printer is sitting in error state for the above. a work around is right clicking the printer in devices and printers and pressing "Remove Device" but obviously can not do this during shutdown.
Is there anyway to remove a device from "Devices and Printers" via Powershell? it does work with network printers but just won't fully remove local printers.
$GetDriver = (Get-Printer -Name "Reports" | Select-Object DriverName | ConvertTo-Csv -NoTypeInformation -Delimiter ",") | % {$_ -replace '"',''} | Select-Object -Skip 1
$GetPort = (Get-Printer -Name "Reports" | Select-Object PortName | ConvertTo-Csv -NoTypeInformation -Delimiter ",") | % {$_ -replace '"',''} | Select-Object -Skip 1
Get-Printer -Name "Reports" | Rename-Printer -NewName $GetDriver
Remove-Printer -Name $GetDriver
Remove-PrinterPort -Name $GetPort
Thanks in advance for any help

Fetching net use details

I am using net use to get the Windows mount path and drive.
PS C:\Users\jagg> net use
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK Y: \\ITHANSJJA001.ABC.COM\opmas$
Microsoft Windows Network
The command completed successfully.
I would like to fetch the drive and mount path detail using PowerShell command. Is there any way to get it using only PowerShell?
you seem to be confused about what powershell is able to do. [grin]
however, here are two ways to get the info you seem to want. the 1st parses the output of net use while the 2nd uses Get-PSDrive to get that same info natively.
(net use) -replace '\s{2,}', ',' |
Select-String -SimpleMatch '\\' |
ConvertFrom-Csv -Header 'Status', 'DriveLetter', 'MountPath', 'Network' |
Select-Object -Property DriveLetter, MountPath
''
Get-PSDrive -PSProvider FileSystem |
# the 4 slashes are 2 regex-escaped slashes
Where-Object {$_.DisplayRoot -match '\\\\'} |
ForEach-Object {
[PSCustomObject]#{
DriveLetter = '{0}:' -f $_.Name
MountPath = $_.DisplayRoot
}
}
hope that helps,
lee

Powershell command to List the services on a specific/individual cluster node

How to find the list of service on a specific node in a given cluster using Powershell?
There are 2 nodes in this cluster group ServerName_1 and ServerName_2, I am trying to fetch the services on Server_name1.
I have tried running the below commands but I did not get any output or error.
I tried using the below command and I could get the results:
PS C:\Users\sd> Get-ClusterGroup | Where-Object {$_.State -EQ "Online"}
Name OwnerNode State
---- --------- -----
Service_1 ServerName_1 Online
Service_2 ServerName_2 Online
However, when i tried to extract the OwnerNode using the same command i do not see any result , as observed below:
PS C:\Users\sd> Get-ClusterGroup | Where-Object {$_.OwnerNode -EQ <ServerName_1>}
PS C:\Users\sd>
As I do not see any output, I am not sure whether the command i executed is correct?
I need this, so that I may start the specific service on ServerName_1 alone.
PS C:\Users\sd> Start-ClusterGroup -Name <ServiceName> | Where-Object
{ $_.OwnerNode -eq "<ServerName1>" }
PS C:\Users\sd>
You don't need the < > in your code, update
Get-ClusterGroup | Where-Object {$_.OwnerNode -EQ <ServerName_1>}
to
Get-ClusterGroup | Where-Object {$_.OwnerNode -EQ "ServerName_1"}
I tested this and it works as expected:
List the ClusterGrop Names-
Get-ClusterNode -Name "Server_name" | Get-ClusterGroup
Also,
List the ClusterGroup Services -
Get-ClusterNode -Name "Server_name" | Get-ClusterResource

How to check if a particular service is running in a remote computer using PowerShell

I have 3 servers, running 3 services:
SERVER-A running serv1.exe service
SERVER-B running serv2.exe service
SERVER-C running serv3.exe service
Using PowerShell how can I query each service to check if they are running and output the information to a text file?
If they all have the same name or display name you can do it in one command. If not you need to run 3 commands.
If all have the same name or display name:
Get-Service -ComputerName server-a, server-b, server-c -Name MyService |
Select Name, MachineName, Status
If they have different names or display names:
I would do this --
#{
'server-a' = 'service-a'
'server-b' = 'service-b'
'server-c' = 'service-c'
}.GetEnumerator() | ForEach-Object {
Get-Service -ComputerName $_.Name -Name $_.Value
} | Select Name, MachineName, Status
To output to a text file use ... | Set-Content ~\Documents\Service_Status.txt where ... is one of the above.
Note - your account will need to have privileges to query the remote machines.
There are several ways to achieve this. I am using a hash of the values since you mentioned that the server to service mapping is always one to one.
$svrHash = #{"SERVER-01"="winmgmt";"SERVER-02"="Wecsvc"}
$svrHash.Keys
| ForEach-Object {Get-Process -ComputerName $_ -Name $svrHash[$_] -Ea SilentlyContinue}
| Select ProcessName
| Out-File C:\Scripts\test.txt
You need to use the service name and not the .exe name.