Fetching net use details - powershell

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

Related

Get-ChildItem on Multiple Computers, Performance Issues

I'm wanting to improve on my script to be able to accomplish the following:
Scan servers based on get-adcomputer on specific OUs.
Scan each server based on whatever drive letter it has.
Scan each server for log4j.
Export all results to a CSV that identifies the folder path, name of file, and the server that the file was found on.
I have been using the following code to start with:
$Servers = Get-ADComputer -Filter * -SearchBase "OU=..." | Select -ExpandProperty Name
foreach ($server in $Servers){
Invoke-Command -ComputerName $Server -ScriptBlock {
$Drives = (Get-PSDrive -PSProvider FileSystem).Root
foreach ($drive in $Drives){
Get-ChildItem -Path $drive -Force -Filter *log4j* -ErrorAction SilentlyContinue | '
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
#{n="Name";e={$Item}}, `
#{n="Created";e={$Age}},`
#{n="FilePath";e={$Path}},`
#{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
} | Export-Csv C:\Results.csv -NoType
}
}
I am having the following issues and would like to address them to learn.
How would I be able to get the CSV to appear the way I want, but have it collect the information and store it on my machine instead of having it on each local server?
I have noticed extreme performance issues on the remote hosts when running this. WinRM takes 100% of the processor while it is running. I have tried -Include first, then -Filter, but to no avail. How can this be improved so that at worst, it's solely my workstation that's eating the performance hit?
What exactly do the ` marks do?
I agree with #SantiagoSquarzon - that's going to be a performance hit.
Consider using writing a function to run Get-ChildItem recursively with the -MaxDepth parameter, including a Start-Sleep command to pause occasionally. Also, you may want to note this link
You'd also want to Export-CSV to a shared network drive to collect all the machines' results.
The backticks indicate a continuation of the line, like \ in bash.
Finally, consider using a Scheduled Task or start a powershell sub-process with a lowered process priority, maybe that will help?

Merge Powershell Output - Get-VM, Get-VHD

I would like to export some Server Statistics from a Failover Cluster System.
My plan is to show with the Get-VHD command all VHD used on the VMs on my host.
So I was Trying to use:
Get-VM | select-object VMID |get-vhd |ft
This gives me a list the
"ComputerName, Path, VHDFormat,VHDType,FileSize,MinimumSize, LogicalSectorSize, PhysicalSectorSize"
Sadly the ComputerName is not the VMName but just the Name of the Host.
Now when I run the Get-VM command I get the Name and here it is actually the VMName.
Is there a nice way to get the real VMnames in the Output of Get-VHD?
I am fairly new to Powershell and I could not find a solution that worked... Most of the "sniplets" found here did not work at all or did not return the VMname...
Thank you for your suggestions =)
Sorry, I think my Title is not that well described, I was not sure how this funktion is called.
Untested, but you could use a ForEach-Object loop and capture the Name property from the Get-VM cmdlet. Then go on with Get-VHD and combine the output:
Get-VM | ForEach-Object {
$name = $_.Name
$_ | Get-VHD | Select-Object #{Name = 'Name'; Expression = {$name}}, *
} | Format-Table -AutoSize

Out-File: output file path is not accepted

Still learning and am having a hard time trying to output information to a file: the output file path is not accepted.
My location is PS Cert:\localmachine and here is the entire command:
$cert = Get-ChildItem -Path cert: -Recurse | where { $_.notafter -le (get-date).AddDays(75) -AND $_.notafter -gt (get-date)} | select notafter, issuer, thumbprint, subject | sort-object notafter
$cert | Out-File -FilePath \\ad.dcpds.cpms.osd.mil\WinAdm\Logs\Expiring_Certificates\$hostname.log
The error message I'm getting is:
Out-File : Cannot open file because the current provider (Microsoft.PowerShell.Security\Certificate) cannot open a file.
Based on the comments above, the issue comes from the fact that the current location is somewhere in the certificate provider (cert:).
One possible workaround/solution is to change the current location back to the file provider before writing the file.
$cert = Get-ChildItem -Path cert: -Recurse | where { $_.notafter -le (get-date).AddDays(75) -AND $_.notafter -gt (get-date)} | select notafter, issuer, thumbprint, subject | sort-object notafter
Set-location c:
$cert | out-file -FilePath \\ad.dcpds.cpms.osd.mil\WinAdm\Logs\Expiring_Certificates\$h‌​ostname.log
Second solution: use a path that explicitly includes the filesystem provider:
$cert | out-file -FilePath FileSystem::\\ad.dcpds.cpms.osd.mil\WinAdm\Logs\Expiring_Certificates\$h‌​ostname.log
To complement PoorKenny's effective solutions with background information:
If you use Out-File and the current location is on a drive of a provider OTHER than the filesystem provider:
only drive letter-based paths are recognized as filesystem paths; e.g.:
... | Out-File C:\temp\out.txt # OK, due to using filesystem drive C:
any other path requires prefix FileSystem::, notably including paths such as \path\to\... and even \\server\share\path\to\... (UNC paths); without the prefix, they're interpreted as relative to the current location, whatever its provider, which fails for any provider other than the filesystem provider.
... | Out-File \temp\out.txt # NOT recognized
... | Out-File \\server\share\temp\out.txt # NOT recognized
... | Out-File FileSystem::\temp\out.txt # OK, thanks to 'FileSystem::' prefix
Arguably, given that Out-File only ever creates files, it would make sense to ALWAYS interpret the -FilePath / -LiteralPath arguments as a filesystem path, irrespective of the provider of the current location.
However, the following, from an example that comes with the Out-File help, suggests that the behavior is by design (the (omitted) example invokes Out-File from a current location on the registry provider's drive).
Because Out-File is not supported by the Windows PowerShell Registry provider, you must specify either the file system drive name, such as c:, or the name of the provider followed by two colons, FileSystem::, in the value of the FilePath parameter.
"
If anyone knows whether there truly is a good reason not to always default to the filesystem provider's current location, do let us know.
(Can there be additional, alternative filesystem providers?).

how to make Powershell output file path using mapped drive path instead of true path?

How can I change the path in result using mapped drive instead of true path? Now I got something like \\server\data\work\.... I would like to see it in lets say K:\work\....
I can't use mapped drive path for variable because it does not work with task scheduler....
Get-ChildItem -Recurse $source -Filter *.prt | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending | select name,LastWriteTime,Directory | convertto-html -head $a -body "<H2>FILES LIST FOR PAST 7 DAYS</H2>" | out-file $output\result.htm
The mapped drive letter should work as long as you're running the Powershell script as a user who has the drive mapped. Otherwise, you need to use New-PSDrive to map the drive for the session.
New-PSDrive –Name K –PSProvider FileSystem –Root "\\server\data"
Try creating a PSDrive at the start of your script. Make sure that the account running the script(through task scheduler) has the required rights on the share.
New-PSDrive –Name "K" –PSProvider FileSystem –Root "\\server\data"
#.... something something, creating $output variable etc.
Get-ChildItem -Recurse $source -Filter *.prt | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending | select name,LastWriteTime,Directory | convertto-html -head $a -body "<H2>FILES LIST FOR PAST 7 DAYS</H2>" | out-file $output\result.htm

Rename Services & Application in Failover Cluster Manager Using Powershell

PS C:\Users\sup> Get-ClusterGroup | Where-Object {$_.name -ilike "*scvmm*"}
Name Owner State
---- --------- -----
scvmm..rtrtry.exported n01 Offline
scvmm..rtrtry566.exported n02 Offline
Hi All, i wanted to rename the services & application in Failover cluster manager which have the words scvmm & exported in it using powershell, for example the above names should be changed to ..rtrtry. & ..rtrtry566., could some one help me out?
You can get the objects you want with a regex match and set the name property:
get-clustergroup |
where {$_.Name -match "scvmm\.(.*)\.exported"} |
foreach {$_.Name = $matches[1]}