Search code examples
powershellpowershell-cmdlet

Having trouble with 'Get-Credentials' on Powershell Script


I have created the below Powershell script, but I recieve 'Access Denied/RPC Server is unavailable' errors upon querying these remote VMs. I can't start the WMI and was hoping to add in some code to prompt for my Windows Credenaitls as this works remotely, just not via Powershell, as a workaround.

Could this be an issue with a Port? Would the WMI service being Stopped, stop me from sing this even with my Credentials?

As this script will be used by colleuges, I'd like to simply add a prompt with no default Username/Password, but upon adding $Cred = Get-Credentials I recieve the error;

Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingExpressionAfterToken

My code is as follows - I also need help adding the part -credentials $Cred (if that's correct);

Param(
$Cred = Get-Credentials,
  [string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",
  [array]$servers = @("Svr1","Svr2","Svr3")
)

Function Get-UpTime 
{ Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
    $os = Get-WmiObject -class win32_OperatingSystem -cn $s
     New-Object psobject -Property @{  
       Uptime = ((get-date) - $os.converttodatetime($os.lastbootuptime)).Days 
       Server=$s  
         }
        }     
       } 

Function Get-DiskSpace 
{ 
 Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     Get-WmiObject -Class win32_volume -cn $s |
       Select-Object @{LABEL='Server';EXPRESSION={$s}}, 
         driveletter, label,
         @{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}}, 
         @{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
         @{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
         @{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}           
    }
}

Function Get-MemoryUsage 
{ 
 Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     Get-WmiObject -Class win32_OperatingSystem -cn $s |
       Select-Object @{LABEL='Server';EXPRESSION={$s}}, 
         @{LABEL=" Total Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvisiblememorysize / 1024))}},
         @{LABEL=" Free Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.freephysicalmemory / 1024))}},
         @{LABEL=" Total Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvirtualmemorysize / 1024))}}, 
         @{LABEL=" Free Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.freevirtualmemory / 1024))}}     
    }
}

$upTime = Get-UpTime -servers $servers |  
ConvertTo-Html -As Table -Fragment -PreContent " 
  Created on: $(get-date)
  <h2>Server Uptime</h2> " | Out-String 

$disk = Get-DiskSpace -servers $servers |  
ConvertTo-Html -As Table -Fragment -PreContent " 
  <h2>Disk</h2> "| Out-String

$memory = Get-MemoryUsage -servers $servers |  
ConvertTo-Html -As Table -Fragment -PreContent " 
  <h2>Memory Usage</h2> "| Out-String    

$head = @'
<style media='screen'>
body {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}

table{
border-collapse: collapse;
border: none;
font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
color: black;
margin-bottom: 10px;
}

table td{
font-size: 12px;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}

table th {
font-size: 12px;
font-weight: bold;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}

h2{ clear: both; font-size: 130%;color:#354B5E; }

h3{
clear: both;
font-size: 75%;
margin-left: 20px;
margin-top: 30px;
color:#475F77;
}

p{ margin-left: 20px; font-size: 12px; }

table.list{ float: left; }

table.list td:nth-child(1){
font-weight: bold;
border-right: 1px grey solid;
text-align: right;
}

table.list td:nth-child(2){ padding-left: 7px; }
table tr:nth-child(even) td:nth-child(even){ background: #BBBBBB; }
table tr:nth-child(odd) td:nth-child(odd){ background: #F2F2F2; }
table tr:nth-child(even) td:nth-child(odd){ background: #DDDDDD; }
table tr:nth-child(odd) td:nth-child(even){ background: #E5E5E5; }
div.column { width: 320px; float: left; }
div.first{ padding-right: 20px; border-right: 1px grey solid; }
div.second{ margin-left: 30px; }
table{ margin-left: 20px; }
–>
</style>
<style media='print'>

</style>
'@

ConvertTo-Html -Head $head -PreContent "<h1>Daily Status Report - LON3 Web Servers</h1>" -PostContent $upTime, $disk, $memory >> $path  
Invoke-Item $path

Solution

  • Credential without S, function inside parenthesis.

    Param(
    $Cred = $(Get-Credential),
      [string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",
      [array]$servers = @("Svr1","Svr2","Svr3")
    )
    

    by the way with a little focus on script conventions your parameters declaration would look :

    [CmdLetBinding()]
    Param ( [Parameter(Mandatory=$false,Position=0)][pscredential]$credential = $(Get-Credential),
            [Parameter(Mandatory=$false,Position=1)][string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html", 
            [Parameter(Mandatory=$false,Position=2)][array]$servers = @("Svr1","Svr2","Svr3")