So i have a very simple powershell command that is looking for a specific file extension on a remote server. These remote servers have multiple logical drives (c:,e:,d: ect...) and when i run this command it works against the C: drive of the server but any drives afterwards return a single error Get-ChildItem : Cannot find drive. A drive with the name 'D' does not exist.
Command is this, any help is appreciated:
Get-WmiObject -ComputerName chsccm10 win32_logicaldisk -Filter "DriveType = 3" | Select-Object DeviceID | ForEach-Object {Get-ChildItem ($_.DeviceID + "\") -Include *.log -Recurse } | select VersionInfo
Haven't tested, but maybe something like this:
$servers = gc c:\serverlist.txt
$results = $servers | %{
invoke-command -cn $_ -scriptblock {
gwmi win32_logicaldisk -filter "DriveType = 3" |
select-object DeviceID |
Foreach-object {
Get-childitem ($_.DeviceID + "\") -include *.log -recurse } |
select VersionInfo}}
$results | out-gridview
Related
I have a script that lists all installed applications on all the servers I have on my network.
The problem is that I am unable to list server name. How do I add $server.name to the select statement so that it is the first column in the output?
# Pinging the server in Powershell-way!
if (Test-Connection -ComputerName $server.name -count 1 -Quiet ) {
echo -computername
echo $server.name
echo $services = $services,
(Get-WmiObject -ComputerName $server.name win32_product | select name, vendor, version | Export-CSV -Path $File -Force -Append -Verbose)
}
you should replace:
(Get-WmiObject -ComputerName $server.name win32_product | select name, vendor, version | Export-CSV -Path $File -Force -Append -Verbose)
With:
(Get-WmiObject -ComputerName $server.name win32_product | select pscomputername, name, vendor, version | Export-CSV -Path $File -Force -Append -Verbose)
/edit, yes PScomputer name is what you are after, in futureuse get-member have a look at your options
I was too quick to ask the question.
There is a variable called PSComputerName in win32_product which is the meachine name
I am trying to provide a list of servers in our domain that lists 2003 and 2008/r2 servers. Along with this information i would like to give the current status of their individual C Drive "free space" & "size of the disk". The script below runs fine and prints a list of all the correct operating systems - But...
The free space and Size are all identical..it gives the first servers drive status and replicates this untill the script finishes. for example the script prints:
serverName1 Windows server 2003 standard deviceid=c freespace=40gb size=12gb
serverName2 Windows server 2008r2 standard deviceid=c freespace=40gb size=12gb
....
serverName100 ..... freespace=40gb size=12gb
Import-Module activedirectory
$2008LogPath = "e:/2008servers.txt"
$2003LogPath = "e:/2003servers.txt"
$servers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
foreach ($server in $servers) {
if($server.OperatingSystem -match "Windows Server 2008") {
Get-WmiObject win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2008LogPath }
elseif($server.operatingsystem -match "Windows Server 2003") {
Get-WmiObject win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2003LogPath }
}
You'll need to use the -ComputerName parameter of the Get-WmiObject cmdlet, to retrieve information from those remote computers. If you don't specify the -ComputerName parameter, then you're retrieving WMI data from the local computer.
To fix this, change your foreach loop to look like the following:
foreach ($server in $servers) {
if($server.OperatingSystem -match "Windows Server 2008") {
Get-WmiObject -ComputerName $Server.Name -Class win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2008LogPath }
elseif($server.operatingsystem -match "Windows Server 2003") {
Get-WmiObject -ComputerName $Server.Name -Class win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2003LogPath }
}
I'm coding a little script with Powershell.
The script is getting the drive letters and exports them with some additional code to the temp directory in a batch file.
Here's an example:
$TempFolderSavePath = $env:temp + "\SDelete.cmd"
Get-PSDrive -PSProvider 'FileSystem' | Select-Object Name | foreach { $_.Name } | ForEach-Object {"SDelete -z "+ $_} | Out-File $TempFolderSavePath
The problem is, that the script is also including mapped network drives.
How I can only use the local drives?
Here's a version of the script without generating a new file and just outputting it to the console.
Get-PSDrive -PSProvider 'FileSystem' | Select-Object Name | foreach { $_.Name } | ForEach-Object {"SDelete -z "+ $_} | Write-Host
As far as I know, Get-PSDrive doesn't know the difference betwee network and local drive. You can use Get-WMIObject Win32_LogicalDisk, which supports filtering drives by type.
Filtering example:
Get-WmiObject Win32_LogicalDisk | select-object DeviceID, DriveType, #{Name="Type";Expression={[IO.DriveType]$_.DriveType}} | ? {$_.Type -eq 'Fixed'}
or
Get-WmiObject Win32_LogicalDisk | ? {$_.DriveType -eq 3}
I've been working on a simple script to read the win32_product off a remote PC, which is working fine. However, I would like the query to ignore some common applications on my domain. I've been building a list of apps and their IdentifyingNumber and putting the IdentifyingNumber into a txt file. I load the text file into a variable with the script and I'm trying to figure out how to get the query to filter each item in the variable...so I have this::
$PC = Read-Host "What is target workstation..."
$logfile = "d:\$PC.txt"
$ignore = [IO.File]::ReadAllText("D:\INCOMING\AppListing\ignore.txt")
get-wmiobject -class win32_product -computer $PC | where {$_.IdentifyingNumber -notlike $ignore} | Select Name, IdentifyingNumber | sort-object Name | export-csv $logfile -encoding "unicode"
However, this is not filtering at all, not even the first or last item from the txt file. I used write-host $ignore to verify it is loading the items...but I am at a lost as to how to make this work. Perhaps a foreach loop? I can't find anything about putting a foreach loop into a where filter though...
Thanks for the assistance...
If the file is like this:
aRandomId
anotherRandonId
...
with one id on each line and nothing else, then try this using -notlike with wildcards on the ends. Ex:
$PC = Read-Host "What is target workstation..."
$logfile = "d:\$PC.txt"
$ignore = [IO.File]::ReadAllText("D:\INCOMING\AppListing\ignore.txt")
get-wmiobject -class win32_product -computer $PC | where { $ignore -notlike "*$($_.identifyingnumber)*" } |
Select Name, IdentifyingNumber | sort-object Name | export-csv $logfile -encoding "unicode"
You could also read your file as an array using ReadAllLines like you would have had to do if you wanted to use a foreach-loop or -notcontains. Ex:
$PC = Read-Host "What is target workstation..."
$logfile = "d:\$PC.txt"
$ignore = [IO.File]::ReadAllLines("D:\INCOMING\AppListing\ignore.txt")
get-wmiobject -class win32_product -computer $PC | where { $ignore -notcontains $_.identifyingnumber } |
Select Name, IdentifyingNumber | sort-object Name | export-csv $logfile -encoding "unicode"
$prods = Compare-Object -ReferenceObject (Get-Content $file) -DifferenceObject ((Get-WmiObject -Class Win32_Product -Computer $computer).IdentifyingNumber) -PassThru
Compare-Object is a great Cmdlet.
I can only get the command to return the services on the first computer in the text file.
Is there a better way than for-each for this task?
Get-Service *vault* -ComputerName (Get-Content c:\users\sean\desktop\js.txt) | select name,status,machinename | sort machinename | format-table -autosize
Try it without the get-content. Try this:
Get-Service *vault* -ComputerName c:\users\sean\desktop\js.txt | select name,status,machinename | sort machinename | format-table -autosize
If that doesn't work, then try:
$Computers = Get-Content c:\users\sean\desktop\js.txt
Get-Service *vault* -computername $Computers | Select name,status,machinename |sort machinename |format-table -autosize
If you are eager for a one-liner then try this:
Get-Content c:\users\sean\desktop\js.txt | Get-Service *vault* | Select name,status,machinename |sort machinename |format-table -autosize
I would try the top one first. I would test, but I don't have access to anything I can do a proper test right now.
$Computers = get-content .\desktop\test.txt
$Service = "Vault"
foreach ($computer in $computers) {
$computer
$Servicestatus = get-service -name $Service -ComputerName $computer
}
$Servicestatus | select-object Name,Status,MachineName | format-table -Autosize
This works for me, it gives me each of the computers in the text file, and it looks for the service.
This is what I use. I get the list of computers from an OU in AD.
Import-Module ActiveDirectory
$ou = "OU=Servers,DC=Domain,DC=com"
$servers = Get-ADComputer -Filter * -SearchBase $ou | select-object -expandproperty name
Foreach ($server in $servers){
$Data = Get-Service -ServiceName *IIS*,*TomCat*,*httpd* -ComputerName $server | select machinename,name | sort machinename | format-table -AutoSize
Write($Data) | Out-File .\WebServices.txt -Append
}
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service -ComputerName $Server -name -like "*vault*"
"-------------------"
}
Following a memory limitation limit with older versions of PowerShell, I was required to refresh my code:
Old code:
gwmi win32_service -computer $allcomputers | Select-Object __SERVER,Name,state,startmode,StartName
New code:
`$servers = Get-Content "computers.txt"
Foreach ($server in $servers) {
Get-WmiObject -Class WIN32_service -ComputerName $server |
Select-Object __SERVER,Name,state,startmode,StartName |
Export-Csv -path "Report.CSV" -NoTypeInformation -Append
}`
This is how you can get list of all services in your AD domain:
Get-ADComputer -Filter {OperatingSystem -Like “Windows 10*”} | ForEach-Object {Get-WmiObject -Class Win32_Service -Computer $_.Name}
More useful examples on this (get list of services for all computer listed in a text file, etc.):
https://www.action1.com/kb/list_of_services_on_remote_computer.html
Get-Service -ComputerName ... has a bug in PowerShell 2.0 that only returns the first computer. This is fixed in newer versions so if you upgrade to PowerShell 3.0 or newer, your original code will work fine.
As a workaround, use a foreach-loop to run Get-Service once for each computer:
Get-Content c:\users\sean\desktop\js.txt |
ForEach-Object { Get-Service -Name *vault* -ComputerName $_ } |
Select-Object -Property Name, Status, MachineName |
Sort-Object -Property MachineName |
Format-Table -AutoSize
Nick's solution totally doesn't work for me. I ended up writing a quick and dirty one that works:
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service *vault*
"-------------------"
}