Declaring parameters and combining them

Welcome Forums General PowerShell Q&A Declaring parameters and combining them

Viewing 1 reply thread
  • Author
    Posts
    • #185282
      Participant
      Topics: 3
      Replies: 2
      Points: 44
      Rank: Member

      Hello
      I just started to play around HTML outputs in Powershell and when i read those cmdlets and parameters, my eyes are bleeding as yours will be. My point is that there are some parameters to combine some parameters but i couldn't figure out how. I think that i shouldn't declare parameters for every case.
      Ps: and Yes, i copied some cmdlets from here but i am just learner.

      
      #adding a function to script and describing functionality of function and its parameters.
      
      function Get-DisksSPaceReport {
      [CmdletBinding()]
      param(
      [Parameter(ValueFromPipeline=$true)]
      [ValidateNotNullOrEmpty()]
      [System.String[]]
      $ComputerName = $env:COMPUTERNAME)
      process {
      $diskList = get-wmiobject Win32_LogicalDisk -Filter "DriveType = 3"
      foreach ($disk in $diskList){
      [PSCustomObject]@{
      ComputerName = $disk.PSComputerName
      DriveLetter = $disk.DeviceID
      VolumeName = $disk.VolumeName
      Size = $disk.Size
      FreeSpace = $disk.FreeSpace }}}}
      
      $date = get-date
      
      #params directing inputs and variables as HTML outputs according to css.
      $params = @{'As'='Table';
      'PreContent'='

      SERVIS LOGS

      '; 'MakeTableDynamic'=$true; 'TableCssClass'='grid'; 'Properties'='DIsplayname','name',@{n='Service Status';e={$_.Status};css={if ($_.Status -eq "Stopped") { 'red' } else {'green'}}}} #the google services are converted to HTML according to @params $SPservice = Get-Service -Displayname "*google*" | ConvertTo-EnhancedHTMLFragment @params $params2 = @{'As'='Table'; 'PreContent'='

      EVENT LOGS

      '; 'MakeTableDynamic'=$true; 'TableCssClass'='grid'; 'Properties'='instanceID', @{n='Entry Status';e={$_.Entrytype};css={if ($_.entrytype -eq "warning") { 'red' } else {'orange'}}}} $eventlogs = Get-EventLog System -Newest 25 | ConvertTo-EnhancedHTMLFragment @params2 #params3 declares different values than other params. COnverting capacity and Freespaceses in GB instead of bytes. With if statements, declaring which color they will be assigned by css. $params3 = @{'As'='Table'; 'PreContent'='

      Driver Status

      '; 'MakeTableDynamic'=$true; 'TableCssClass'='grid'; 'Properties'='Computername','DriveLetter', @{Name='Capacity';Expression={[math]::round($_.Size / 1GB,2)}}, @{Name='FreeSpaceIn%';Expression={[math]::round($_.FreeSpace / $_.Size * 100,2)};css={if ($_.Free -gt 100) { 'red '} else {'green'}}}, @{Name='FreeSpaceLessThan10%';Expression={If(($_.FreeSpace / $_.Size * 100) -lt 10){$true}Else{$false}}}, @{Name='FreeSpace';Expression={[math]::round($_.FreeSpace / 1GB,2)}; css={if ($_.Free -gt 100) { 'red '} else {'green'}}}} #getting driver objects $drivers= Get-DisksSPaceReport| Select-Object -Property ComputerName,DriveLetter,VolumeName,Size,FreeSpace | ConvertTo-EnhancedHTMLFragment @params3 #getting all variables in HTML format and applying CSS file and creating a txt file in a particular file. ConvertTo-EnhancedHTML -HTMLFragments " $SPservice $eventlogs $Drivers $date" -CssUri C:\Users\A701026\Desktop\sample\styles2.css |Out-File C:\Users\A701026\Desktop\sample\paramexample.html #creating pop up which shows a warning related with result. $wsh = new-object -com wscript.shell $wsh.popup.overloaddefinitions $wsh.popup(" HTML report is ready. Please check it in chrome browser",10,"Powershell Automation",0+64)
    • #185288
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      Try being a little clearer on what exactly is or is not working, but are you referring to setting the same parameters over and over again? The splatting of parameters is using hash tables, so you can look at managing hash tables to .Add parameters, .Remove parameters or Setting (.Set_Item) on a parameter to leverage a one parameter set and manipulate it.

      #Define original params
      $params = @{
          'As'='Table';
          'PreContent'='SERVIS LOGS';
          'MakeTableDynamic'=$true;
          'TableCssClass'='grid';
          'Properties'='Displayname','Name',@{n='Service Status';e={$_.Status};css={if ($_.Status -eq "Stopped") { 'red' } else {'green'}}}
      }
      
      'First parameter...'
      $params.Get_Item('PreContent')
      
      'Update the PreContent key...'
      $params.Set_Item('PreContent', 'Next Header')
      
      'Second parameter...'
      $params.Get_Item('PreContent')
      

      Output:

      PS C:\Users\rasim\Desktop> C:\Users\rasim\Desktop\test.ps1
      First parameter...
      SERVIS LOGS
      Update the PreContent key...
      Second parameter...
      Next Header
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.