Hello Guys,
I am a powershell novice, learning on V3. After learning all those tutorials, finally I have manged to make an advanced function. It may be ugly but works absolutely fine when I see the output on the console/redirect to CSV. When I want to redirect/capture the output into a variable, the console output is fine, BUT when I lookup the variable, iot is messed up. I don’t know what I am doing wrong and I hope I have explained it better.
So, this function is to collect the partition information on multiple remote machines. My environment has mixed OS.
In this test case, I am using 3 remote servers -
server1 - online server
offline - an offline server
linux - a linux server (running get-wmiobject to this server will return RPCunavailable error)
I made this script to try with several credentials, but I am not using any of those techniques in this test case.
Here is my script -
function Get-Logicaldiskinfo
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $false,Position=0)]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $false)]
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter()]
[switch]$Localpassword
)
Begin
{
$domain = 'company'
try
{
if ( $ComputerName -ne $env:COMPUTERNAME -and $env:USERDOMAIN -ne $domain -and $Credential.UserName -eq $null )
{
$Credential = Get-Credential -Message "Please provide the credentials to be used to be used for remote connection" -ErrorAction SilentlyContinue
}
}
catch
{
#do nothing
}
if ( $Localpassword )
{
$altpassword = Read-Host -AsSecureString -Prompt "Please enter the local admin password"
}
# create a dummy set for the report
$report_dummy = [pscustomobject]@{
Host = '-'
Status = '-'
Partition = '-'
TotalSize = '-'
Freespace = '-'
OSversion = '-'
}
}#end begin
Process
{
foreach ($server in $ComputerName)
{
if ( Test-Connection $server -Count 1 -Quiet)
{
#try with currently loggedon(domain) user
try
{
$wmiparams = @{
'Class' = 'win32_logicaldisk';
'Filter' = 'Drivetype=3';
'ComputerName' = $server;
'Erroraction' = 'Stop';
'Credential' = $Credential;
}
if ($server -eq $env:COMPUTERNAME)
{
$wmiparams.Remove('credential')
$partitions = Get-WmiObject @wmiparams
$wmiparams.Remove('Filter')
$wmiparams.class = 'win32_operatingsystem'
$os = Get-WmiObject @wmiparams
}
else
{
$partitions = Get-WmiObject @wmiparams
$wmiparams.Remove('Filter')
$wmiparams.class = 'win32_operatingsystem'
$os = Get-WmiObject @wmiparams
}
foreach ($partition in $partitions){
$report = [pscustomobject]@{
Host = $server
Status = 'online'
Partition = $partition.Name
TotalSize = "{0:N2}" -f ($partition.Size/1GB) -as [decimal]
Freespace = "{0:N2}" -f ($partition.Freespace/1GB) -as [decimal]
OSVersion = $os.caption + ' ' + $os.csdversion
}
$report
}
} #end try
# Access denied with domain credetials
catch [System.UnauthorizedAccessException]
{
if ($altpassword)
{
#try with support password
try
{
$support = New-Object pscredential -ArgumentList "$server\support",$altpassword
$wmiparams.Credential = $support
$partitions = Get-WmiObject @wmiparams
$wmiparams.Remove('Filter')
$wmiparams.Class = 'win32_operatingsystem'
$os = Get-WmiObject @wmiparams
foreach ($partition in $partitions){
$report = [pscustomobject]@{
Host = $server
Status = 'online'
Partition = $partition.Name
TotalSize = "{0:N2}" -f ($partition.Size/1GB) -as [decimal]
Freespace = "{0:N2}" -f ($partition.Freespace/1GB) -as [decimal]
OSVersion = $os.caption + ' ' + $os.csdversion
}
}
$report
}
#Access denied with support credential
catch [System.UnauthorizedAccessException]
{
#try with administrator password
try
{
$administrator = New-Object pscredential -ArgumentList "$server\administrator",$altpassword
$wmiparams.Credential = $administrator
$partitions = Get-WmiObject @wmiparams
$wmiparams.Remove('Filter')
$wmiparams.Class = 'win32_operatingsystem'
$os = Get-WmiObject @wmiparams
foreach ($partition in $partitions){
$report = [pscustomobject]@{
Host = $server
Status = 'online'
Partition = $partition.Name
TotalSize = "{0:N2}" -f ($partition.Size/1GB) -as [decimal]
Freespace = "{0:N2}" -f ($partition.Freespace/1GB) -as [decimal]
OSVersion = $os.caption + ' ' + $os.csdversion
}
}
$report
}
#Access denied with all three credentials
catch
{
$report = $report_dummy
$report.host = $server
$report.status = 'Access Denied'
$report
}
}
}
else
{
$report = $report_dummy
$report.host = $server
$report.status = 'Access Denied'
$report
}
}
# GWMI RPC unavailable error
catch [System.Runtime.InteropServices.COMException]
{
$report = $report_dummy
$report.host = $server
$report.status = 'RPC Server Unavailable'
$report
}
# all other errors
catch
{
$report = $report_dummy
$report.host = $server
$report.status = 'Unknown Error'
$report
}
} # end if test-connection
else
{
$report = $report_dummy
$report.host = $server
$report.status = 'Unreachable'
$report
}
} # end foreach
} # end process
}
My sample output -
As you can see the the redirected variable output is messed up. (For formatting purpose I have used autosize in ft, otherwise I just use ft so that I can see the script running against each host and its output)
PS [9:52:52 PM] C:\> $output PS [9:52:54 PM] C:\> Get-Logicaldiskinfo Linux, server1, offline | Tee-Object -Variable output | ft -AutoSize Host Status Partition TotalSize Freespace OSversion ---- ------ --------- --------- --------- --------- Linux RPC Server Unavailable - - - - server1 online C: 114.99 97.73 Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition Service Pack 2 offline Unreachable - - - - PS [9:53:32 PM] C:\> $output | ft Host Status Partition TotalSize Freespace OSversion ---- ------ --------- --------- --------- --------- offline Unreachable - - - - server1 online C: 114.99 97.73 Microsoft(R) Windows(R) Server 20... offline Unreachable - - - - PS [9:53:37 PM] C:\> Get-Logicaldiskinfo server1, offline, Linux | Tee-Object -Variable output | ft -AutoSize Host Status Partition TotalSize Freespace OSVersion ---- ------ --------- --------- --------- --------- server1 online C: 114.99 97.73 Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition Service Pack 2 offline Unreachable - - - - Linux RPC Server Unavailable - - - - PS [9:54:13 PM] C:\> $output | ft Host Status Partition TotalSize Freespace OSVersion ---- ------ --------- --------- --------- --------- server1 online C: 114.99 97.73 Microsoft(R) Windows(R) Server 20... Linux RPC Server Unavailable - - - - Linux RPC Server Unavailable - - - -
Sorry again for a really lengthy post. Please suggest.
EDIT - Attaching the screenshot.