Trying to use remote variable in If-Condition within ScriptBlock using PowerShell - powershell-remoting

I am trying to use below code to remove the groups from Active Directory. I am maintaining an input file for the list of servers. the input file is in the local machine and trying to use the content of the file inside the ScriptBlock using ($Using:variablename) after taking a remote session.
I am getting the content[file content] of the variable inside the ScriptBlock which is defined outside the ScriptBlock. The issue is the if condition [if($serverlist -ne $null)] is satisfying even if the variable is empty. The code is entering into if block even if the variable $serverlist is null, instead it has to take else block.
Could you please help me on this.
$adserver = Read-Host -Prompt "Please enter AD server details"
$adserver
$askcredential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")
$credential = New-Object System.Management.Automation.PsCredential($askcredential.UserName,$askcredential.Password)
$session = New-PSSession -ComputerName "$adserver" -Credential $credential
#$command = (Get-ADForest).domains
$addomain = Read-Host -Prompt "Please enter the Domain"
$servers = Get-Content "D:\Bhargavi\Windows-Decommission\$addomain\servers.txt"
#Invoke-Command -ComputerName $adserver -Authentication Default -Credential $credential -ScriptBlock {
Invoke-Command -Session $session -ScriptBlock {
#$addomain = Read-Host -Prompt "Please enter the Domain"
#$servers = Get-Content "D:\Bhargavi\Windows-Decommission\$addomain\servers.txt"
#Param($domainname,$serverlist)
$serverlist = $Using:servers
$domainname = $Using:addomain
Write-Host "the list is $serverlist"
Write-Host "the domain is $domainname"
if($serverlist -ne $null)
{
Write-Host "Found the data in the input file. Proceeding to remove the groups."
foreach ($server in $serverlist)
{
write-host "Server Name is: $server"
$mgserver = "MG-$server"
$mggroup = Get-ADGroup -Filter { Name -like $mgserver }
$mggroup
if($mggroup -ne $null)
{
Get-ADGroup -Filter { Name -like $mgserver } #| Remove-ADGroup -Confirm:$False
$mggroupconfirm = Get-ADGroup -Filter { Name -like $mgserver }
if($mggroupconfirm -eq $null)
{
Write-Host "$mgserver is removed from $domainname"
#Set-Content -Path "D:\Bhargavi\Windows-Decommission\$domainname\servers.txt" -Value (get-content -Path "D:\Bhargavi\Windows-Decommission\$domainname\servers.txt" | Select-String -Pattern "$server" -NotMatch)
}
else
{
Write-Host "$mgserver is not removed. Please try again."
}
}
else{Write-Host "There is no group called $mgserver in $domainname domain "}
}
}
else
{
DO
{
$val = Read-Host -Prompt "Do you want to enter servername ? Enter yes or no"
if($val -eq "yes")
{
$entermgserver = Read-Host -Prompt "Please enter the server name"
$entermgserver
$mgserver = "MG-$entermgserver"
$mgserver
$mggroup = Get-ADGroup -Filter { Name -like $mgserver }
$mggroup
if($mggroup -ne $null)
{
#Write-Host "$mggroup is not null"
Get-ADGroup -Filter { Name -like $mgserver } #| Remove-ADGroup -Confirm:$False
$mggroupconfirm = Get-ADGroup -Filter { Name -like $mgserver }
if($mggroupconfirm -eq $null)
{
Write-Host "$mgserver is removed"
}
else
{
Write-Host "$mgserver is not removed. Please try again."
}
}
else{Write-Host "The is no group called $mgserver in $domainname domain"}
}
}until ($val -eq "no")
}
} ### End Script Block
Clear-Content "D:\Bhargavi\Windows-Decommission\$addomain\servers.txt"
Remove-PSSession -Session $session

Related

Using PowerShell to verify that a specific service exists from a .txt list of server names

I am trying to verify that a specific service exists from a .txt list of server names and then output to a file. In this case I need to also add credentials so I need to use the Invoke-Command. What am I doing wrong here?
clear
start-transcript -path .\Log.txt
$servers = Get-Content .\Resources\Lab.txt
$cred = get-credential domain\Username
$name = Read-Host -Prompt 'Input your service name'
function Getinfo() {foreach($server in $servers)
{
Get-Service | Where-Object { $_.Name -eq $name}-and {$_.Status -eq "Running"}| Format-Table -AutoSize
}
}
Invoke-Command -credential $cred -ComputerName $servers -ScriptBlock ${function:Getinfo}
Stop-Transcript
I think you mean this?
$servers = Get-Content .\Resources\Lab.txt
$cred = Get-Credential domain\Username
$name = Read-Host -Prompt 'Input your service name'
Invoke-Command -Credential $cred -ComputerName $servers -ScriptBlock {
param($name)
Get-Service | Where-Object { $_.Name -eq $name -and $_.Status -eq "Running"}
} -ArgumentList $name | Format-Table -AutoSize

Is it possible to load all functions of a script before it runs?

I have a rather complex script, which follows the following steps;
-->Login (Ask user to enter admin details)
--->Start (Queries Ad for user creditals)
--->Progress (Creates a progress bar)
--->Search (Carries out the search for the data)
--->Question1 -(Yes - Select-Folder No - Create)
--->Select-Folder (Asks the user to create a file path for the document to be stored)
--->Go (Creates a csv from the results of the search)
--->Question2 (CSV - Result XLSX - Excel) (Asks the user if they wish to create a Xlsx file from the Csv)
--->Create (Checks to see if file path exists C:\temp\Server_shares if not creates it)
--->Done (creates csv at default location C:\temp\Server_shares)
--->Question2 (Asks the user if they wish to create a Xlsx file from the Csv)
--->Question2 -(CSV - Result XLSX - Excel) (Asks the user if they wish to create a Xlsx file from the Csv)
--->Result (User has chosen not to create a Xlsx notifys the user of the file path)
--->End (Closes script)
--->Excel (creates Xlsx from csv and stores it in either default or user defined location)
--->Delete (Deletes all remaingin Csv's from file path)
--->End (Closes script)
it seems that I keep get unhandelled exception errors everytime that I run the code.
I have found out that it runs then loads the functions, I need to run them all in memory so that when called each one will run without error.
I have tried creating varibles for each function, but this exactly the same as what I have already.
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName ("Microsoft.VisualBasic")
$Script:ErrorActionPreference = "Stop"
$Script:Ma3 = "C:\Temp\Server_Shares\"
Function Get-Event{
Function Get-Login {
Clear-Host
#Write-Host "Please provide admin credentials (for example DOMAIN\admin.user and your password)"
$Global:Credential = Get-Credential
}
Function Get-Start{
#Get user credentials
$Cred = Get-Credential -Message "Enter Your Credentials (Domain\Username)"
if ($Cred -eq $Null)
{
Write-Host "Please enter your username in the form of Domain\UserName and try again" -BackgroundColor Black -ForegroundColor Yellow
Rerun
Break
}
#Parse provided user credentials
$DomainNetBIOS = $Cred.username.Split("{\}")[0]
$UserName = $Cred.username.Split("{\}")[1]
$Password = $Cred.GetNetworkCredential().password
Write-Host "`n"
Write-Host "Checking Credentials for $DomainNetBIOS\$UserName" -BackgroundColor Black -ForegroundColor White
Write-Host "***************************************"
If ($DomainNetBIOS -eq $Null -or $UserName -eq $Null)
{
Write-Host "Missing domain please type in the following format: Domain\Username" -BackgroundColor Black -ForegroundColor Yellow
Rerun
Break
}
# Checks if the domain in question is reachable, and get the domain FQDN.
Try
{
$DomainFQDN = (Get-ADDomain $DomainNetBIOS).DNSRoot
}
Catch
{
Write-Host "Error: Domain was not found: " $_.Exception.Message -BackgroundColor Black -ForegroundColor Red
Write-Host "Please make sure the domain NetBios name is correct, and is reachable from this computer" -BackgroundColor Black -ForegroundColor Red
Rerun
Break
}
#Checks user credentials against the domain
$DomainObj = "LDAP://" + $DomainFQDN
$DomainBind = New-Object System.DirectoryServices.DirectoryEntry($DomainObj,$UserName,$Password)
$DomainName = $DomainBind.distinguishedName
If ($DomainName -eq $Null)
{
Write-Host "Domain $DomainFQDN was found: True" -BackgroundColor Black -ForegroundColor Green
$UserExist = Get-ADUser -Server $DomainFQDN -Properties LockedOut -Filter {sAMAccountName -eq $UserName}
If ($UserExist -eq $Null)
{
Write-Host "Error: Username $Username does not exist in $DomainFQDN Domain." -BackgroundColor Black -ForegroundColor Red
Rerun
Break
}
Else
{
Write-Host "User exists in the domain: True" -BackgroundColor Black -ForegroundColor Green
If ($UserExist.Enabled -eq "True")
{
Write-Host "User Enabled: "$UserExist.Enabled -BackgroundColor Black -ForegroundColor Green
}
Else
{
Write-Host "User Enabled: "$UserExist.Enabled -BackgroundColor Black -ForegroundColor RED
Write-Host "Enable the user account in Active Directory, Then check again" -BackgroundColor Black -ForegroundColor RED
Rerun
Break
}
If ($UserExist.LockedOut -eq "True")
{
Write-Host "User Locked: " $UserExist.LockedOut -BackgroundColor Black -ForegroundColor Red
Write-Host "Unlock the User Account in Active Directory, Then check again..." -BackgroundColor Black -ForegroundColor RED
Rerun
Break
}
Else
{
Write-Host "User Locked: " $UserExist.LockedOut -BackgroundColor Black -ForegroundColor Green
}
}
Write-Host "Authentication failed for $DomainNetBIOS\$UserName with the provided password." -BackgroundColor Black -ForegroundColor Red
Write-Host "Please confirm the password, and try again..." -BackgroundColor Black -ForegroundColor Red
Rerun
Break
}
Else
{
Write-Host "SUCCESS: The account $Username successfully authenticated against the domain: $DomainFQDN" -BackgroundColor Black -ForegroundColor Green
}
Search
}
Function Rerun {
$Title = "Enter another set of Credentials?"
$Message = "Do you want to try another set of credentials?"
$Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Try Again?"
$No = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "End Script."
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($Yes, $No)
$Result = $host.ui.PromptForChoice($Title, $Message, $Options, 0)
Switch ($Result)
{
0 {Get-Start}
1 {"End Script."}
}
}
Function Get-Progress{
Try{
{If (Test-Path $PC -ErrorAction Stop) {
Add-Type -assembly System.Windows.Forms
## -- Create The Progress-Bar
$ObjForm = New-Object System.Windows.Forms.Form
$ObjForm.Text = "Progress-Bar of searched folders"
$ObjForm.Height = 100
$ObjForm.Width = 500
$ObjForm.BackColor = "White"
$ObjForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$ObjForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
## -- Create The Label
$ObjLabel = New-Object System.Windows.Forms.Label
$ObjLabel.Text = "Starting. Please wait ... "
$ObjLabel.Left = 5
$ObjLabel.Top = 10
$ObjLabel.Width = 500 - 20
$ObjLabel.Height = 15
$ObjLabel.Font = "Tahoma"
## -- Add the label to the Form
$ObjForm.Controls.Add($ObjLabel)
$PB = New-Object System.Windows.Forms.ProgressBar
$PB.Name = "PowerShellProgressBar"
$PB.Value = 0
$PB.Style="Continuous"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 500 - 40
$System_Drawing_Size.Height = 20
$PB.Size = $System_Drawing_Size
$PB.Left = 5
$PB.Top = 40
$ObjForm.Controls.Add($PB)
## -- Show the Progress-Bar and Start The PowerShell Script
$ObjForm.Show() | Out-Null
$ObjForm.Focus() | Out-NUll
$ObjLabel.Text = "Starting. Please wait ... "
$ObjForm.Refresh()
Start-Sleep -Seconds 1
Out-Null
## -- Execute The PowerShell Code and Update the Status of the Progress-Bar
$result = (get-acl $pc).Access
$Counter = 0
ForEach ($Item In $Result) {
## -- Calculate The Percentage Completed
$Counter++
[Int]$Percentage = ($Counter/$Result.Count)*100
$PB.Value = $Percentage
$ObjLabel.Text = "Scanning $Folders For $criteria in $PC"
#$ObjLabel.Text = "Found $counter Pst's in $Search"
$ObjForm.Refresh()
Start-Sleep -Milliseconds 150
# -- $Item.Name
#"`t" + $Item.Path
$ObjForm.Close()
#Write-Host "`n"
Else {
#Write-Host
#Write-Host "`t Cannot Execute The Script." -ForegroundColor "Yellow"
#Write-Host "`t $Search Does Not Exist in the System." -ForegroundColor "Yellow"
#Write-Host
}
}
}
}
}
Catch{
Write-Host "Please enter a vaild path" -ForegroundColor Cyan
Search
}
}
Function Script:Get-Question {
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(200, 100)
$form1.topmost = $true
$Text = New-Object System.Windows.Forms.Label
$Text.Location = New-Object System.Drawing.Point(15, 15)
$Text.Size = New-Object System.Drawing.Size(200, 40)
$Text.Text = "Would you like to save the file to a custom location?"
$Form1.Controls.Add($Text)
#$ErrorActionPreference = "SilentlyContinue"
Function Button1
{
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Point(20, 55)
$Button1.Size = New-Object System.Drawing.Size(55, 20)
$Button1.Text = "Yes"
$Button1.add_Click({Get-Go -ErrorAction SilentlyContinue
$Form1.Close()})
$Form1.Controls.Add($Button1)
}
Function Button2
{
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Point(80, 55)
$Button2.Size = New-Object System.Drawing.Size(55, 20)
$Button2.Text = "No"
$Button2.add_Click({Get-Create -ErrorAction SilentlyContinue
$Form1.Close()})
$Form1.Controls.Add($Button2)
}
Button1
Button2
[void]$form1.showdialog()
}
Function Select-FolderDialog{
param([string]$Description="Select Folder",[string] $RootFolder="Desktop")
[System.Reflection.Assembly]::LoadWithPartialName ("System.windows.forms") | Out-Null
Write-host "Please minimize the console to select a folder in which to save the results"
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Rootfolder = $RootFolder
$objForm.Description = $Description
$objForm.ShowNewFolderButton = $false
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.SelectedPath
}
Else
{
Write-Error "Operation cancelled by user."
Exit
}
}
Function Get-Search{
Write-host "Please Minimize the console and enter the full folder path that you require permissions for" -ForegroundColor Green
$Script:PC = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the full path of the folder you wish to search", "Folder choice")
If ($PC -eq "")
{
Exit
}
Get-Progress
$Res = (get-acl $pc).Access
$Script:Gold = $Res| Select-object #{label = "User Groups";Expression = {$_.IdentityReference}},
#{label = "Rights";Expression = {$_.FileSystemRights}},
#{label = "Access";Expression = {$_.AccessControlType}}
Get-Question
}
Function Script:Get-Go{
$Form1.Close()
$FPath = Select-FolderDialog
$Folder = $FPath + "\" + [Microsoft.VisualBasic.Interaction]::InputBox ("Please select a folder to save the data to", "Path Choice") + "\"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
"Please minimize the console to select a folder in which to save the results"
$Name = [Microsoft.VisualBasic.Interaction]::InputBox("Please choose a filename", "File Name Choice")
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$cfgOutpath = $Folder + "$Name"
if( -Not (Test-Path -Path $Folder ) )
{
New-Item -ItemType directory -Path $Folder |out-null
}
Else{
[System.Windows.MessageBox]::Show('The directory already exists','Error','Ok','Error')
}
$Gold | Export-Csv "$cfgOutpath.csv" -NoClobber -NoTypeInformation
Write-Host "File has been saved to $cfgOutpath.csv" -ForegroundColor Yellow
Get-Q2
}
##############################################
## Testing Phases ##
## Get-Start ##
## ##
##############################################
Search
Function Script:Get-Create {
$Form1.Close()
if( -Not (Test-Path -Path $Ma3 ) )
{
New-Item -ItemType directory -Path $Ma3 |out-null
}
Done
}
Function Script:Get-Done{
$PC2 = ($PC -split '\\')[-1]
$CSV = "C:\Temp\Server_Shares\User access for $PC2"
$cfgOutpath = $CSV
$Gold | Export-Csv "$cfgOutpath.csv" -NoClobber -NoTypeInformation
Get-Q2
}
Function Script:Get-Q2 {
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form2 = New-Object System.Windows.Forms.Form
$Form2.ClientSize = New-Object System.Drawing.Size(200, 100)
$form2.topmost = $true
$Text = New-Object System.Windows.Forms.Label
$Text.Location = New-Object System.Drawing.Point(15, 15)
$Text.Size = New-Object System.Drawing.Size(200, 40)
$Text.Text = "Would you like to create an Xlsx document or leave it as csv?"
$Form2.Controls.Add($Text)
$ErrorActionPreference = "SilentlyContinue"
Function Button1
{
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Point(20, 55)
$Button1.Size = New-Object System.Drawing.Size(55, 20)
$Button1.Text = "CSV"
$Button1.add_Click({Get-Result
$Form2.Close()})
$Form2.Controls.Add($Button1)}
Function Button2
{
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Point(80, 55)
$Button2.Size = New-Object System.Drawing.Size(55, 20)
$Button2.Text = "XLSX"
$Button2.add_Click({Get-Excel
$Form2.Close()})
$Form2.Controls.Add($Button2)
}
Button1
Button2
[void]$form2.showdialog()
}
Function Script:Get-Ans{
$Form2.Close()
Try{
Get-Excel
}
Catch{
Write-Host "Unable to create XSLX please check full path." -ForegroundColor Red
}
}
Function Script:Get-Result{
$Form2.Close()
Write-Host "File has been saved to $CSV.csv" -ForegroundColor Yellow
}
Function Script:Get-Excel{
$RD = $Ma3 + "*.csv"
$CsvDir = $RD
$csvs = dir -path $CsvDir # Collects all the .csv's from the driectory
$outputxls = "$Ma4.Xlsx"
$Excel = New-Object -ComObject excel.application
$Excel.displayAlerts = $false
$workbook = $excel.Workbooks.add()
# Loops through each CVS, pulling all the data from each one
foreach($iCsv in $csvs){
$iCsv
$WN = ($iCsv -Split "\\")[5]
$wn = ($WN -Split " ")[3]
$WN = $WN -replace ".{5}$"
$Excel = New-Object -ComObject excel.application
$Excel.displayAlerts = $false
$Worksheet = $workbook.worksheets.add()
$Worksheet.name = $WN
$TxtConnector = ("TEXT;" + $iCsv)
$Connector = $worksheet.Querytables.add($txtconnector,$worksheet.Range("A1"))
$query = $Worksheet.QueryTables.item($Connector.name)
$query.TextfileOtherDelimiter = $Excel.Application.International(5)
$Query.TextfileParseType =1
$Query.TextFileColumnDataTypes = ,2 * $worksheet.cells.column.count
$query.AdjustColumnWidth =1
$Query.Refresh()
$Query.Delete()
$Worksheet.Cells.EntireColumn.AutoFit()
$Worksheet.Rows.Item(1).Font.Bold = $true
$Worksheet.Rows.Item(1).HorizontalAlignment = -4108
$Worksheet.Rows.Item(1).Font.Underline = $true
$Workbook.save()
}
$Empty = $workbook.worksheets.item("Sheet1")
$Empty.Delete()
$Workbook.SaveAs($outputxls,51)
$Workbook.close()
$Excel.quit()
Write-Host "File has been saved to $outputxls" -ForegroundColor Yellow
Delete
}
Function Script:Delete{
get-childitem $MA3 -recurse -force -include *.txt | remove-item -force #Removes all txt files from final directory
get-childitem $MA3 -recurse -force -include *.csv | remove-item -force #Removes all CSV files from final directory
}
Write-Host "Finished"
}
Get-Event
#Excel-Write'
I know it's a lot of code, but in-order for anyone to replicate the probelm, you will need it all.
I want it to run, first time, everytime, at the moment it take 3-4 tries before its loaded each function into memory.
Put all of your functions into a PowerShell module file.
C:\Module.psm1
And then import that module before anything else in the script:
Import-Module C:\Module.psm1
For larger scripts this makes things more manageable as you can keep your long list of functions separate from the script that calls them.

How to pass multiple domain and local user accounts into a function

I have a script that I have been trying to massage and I want to take a loop that was copied multiple times for each user and I want to turn it into a function.
I have figured out to pass multiple local users to the script and I have been able to pass one domain user to the script and have it work successfully.
But I want to be able to create a list of users and their domains (some have none)
and pipe that into the function automatically. I know I could just keep writing out the function with each username and password but If I can avoid that, that would be great
Function Launch-cfm {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $username,
[Parameter(Mandatory=$false, Position=1)]
[string] $domain
)
if ($domain -eq $tue) {
Stop-Process -name "autohotkey" -Force -ErrorAction SilentlyContinue
&$OutFile
$user = "$username"
$user_sam = ($members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -contains $user
if ($user_sam -eq $true) {
$user = "$username"
$account = $user
$PassFile = $CredPath+$user+,"_Password.txt"
$keyFile = $CredPath+$user+,".key"
$key = Get-content $keyFile
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $account, (Get-Content $PassFile | ConvertTo-SecureString -Key $key)
Write-Host "info to user about scripts actions."
C:
Start-Process -FilePath $mmcPath -ArgumentList $mscPath -Credential $cred;pause
} else { Write-Host "$user does not exist on this server!!!! Moving on...!
"}
} else {
Stop-Process -name "autohotkey" -Force -ErrorAction SilentlyContinue
&$OutFile
$user = "$username"
$user_sam2 = ($members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -contains $user
if ($user_sam2 -eq $true) {
$account = $domain+,"\"+$user
$PassFile = $CredPath+$user+,"_Password.txt"
$keyFile = $CredPath+$user+,".key"
$key = Get-content $keyFile
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $account, (Get-Content $PassFile | ConvertTo-SecureString -Key $key)
Write-Host "info to user about scripts actions"
Start-Process -FilePath $mmcPath -ArgumentList $mscPath -Credential $cred;pause
} else { Write-Host "$user does not exist on this server!!!! Moving on...!
"}
}
}
$use = "User1","user2"
$dom = "domain1",""
launch-cfm -username $use -domain $dom
any suggestion would be great. or to know if what I am asking is even possible.
Thanks.
What I think you are looking for is a never ending parameter. Give this a try.
Input: Launch-cfm -usernames "Drew","Cleadus","Stack" -domain "SuperDomain1337"
Function Launch-cfm {
Param (
[Parameter(Mandatory=$true)]
[string[]] $usernames,
[Parameter(Mandatory=$false)]
[string] $domain
)
Foreach($user in $usernames){
Do-Magic
}
}
Reasoning:
I am not a fan of positional parameters, throw them where they feel right in the moment.
Using [string[]] instead of [string] means that it will put all values passed to it into an array for later use within the function. This current configuration allows for MULTIPLE users but only ONE domain. You can change that but would need to iterate over each domain and user at a time, unless specified within the script some how.
EG.
Foreach($dom -in $domain){
Foreach($user in $usernames){
Do-Magic
} Else {
Do-LessImpressiveMagic
}
}

How to apply colors in powershell output

Requirement :
I am beginner in powershell. Below ps script is giving the details about services are in started state or in stopped state but my requirement is I need to see this out put as background color in 'Sky Blue', if services are running then highlight in Green ,Stopped services in Red color. How do I achieve it.
Help on this is highly appriciated.
$Result = #()
foreach($server in Get-Content C:\PowerSQL\List.txt)
{
$Services=gwmi win32_service -computername $server | where {$_.Name -like ‘*SQL*’}
if(!(Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
{“Problem still exists in connecting to $server”}
ELSE {
$services | ForEach {
If ($_)
{ $Result += New-Object PSObject -Property #{
‘Host Name’ = $_.Systemname
‘Service Display Name’ = $_.Displayname
‘Service Name’ = $_.Name
‘Start Mode’ = $_.Startmode
‘Service Account Name’ = $_.Startname
‘State’ = $_.State
‘Status’= $_.Status
}
}
}
}
}
$Result | ConvertTo-HTML | Out-File C:\PowerSQL\service.htm
See my answer to similar question to this.
Communary.ConsoleExtensions [link] might help you
Invoke-ColorizedFileListing C:\Windows -m *.dmp
The above command will colorise file types and highlight dump files.
To save a color output, you would have to save to a format that preserves color, like RTF, or HTML. Txt (plain text file) only stores text.
The code below will save your output as an html file.
$time = (Get-Date).AddYears(-2)
Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -lt $time} |
Select Directory,Name,LastWriteTime |
ConvertTo-Html -Title "Services" -Body "<H2>The result of Get-ChildItem</H2> " -Property Directory,Name,LastWriteTime |
ForEach-Object {
if ($_ -like '<tr><td>*') {
$_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4'
} else {
$_
}
} | Set-Content "$env:TEMP\ColorDirList.html" -Force
The line:
if ($_ -like '<tr><td>*') {
...checks for line in the html output that is a table row.
The line:
$_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4'
...uses a RegEx to replace the 2nd table cell contents with a font tag with the color green. This is a very simple RegEx search & replace that will only color the 2nd column.
And here's another implementation of console only coloring, based on this link
$linestocolor = #(
'CSName Version OSArchitecture'
'------ ------- --------------'
'BENDER 6.1.7601 64-bit '
'LEELA 6.1.7601 64-bit '
'FRY 6.1.7600 64-bit '
'FARNSWORTH 6.1.7601 32-bit '
)
# http://www.bgreco.net/powershell/format-color/
function Format-Color {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true)]
$ToColorize
, [hashtable]$Colors=#{}
, [switch]$SimpleMatch
, [switch]$FullLine
)
Process {
$lines = ($ToColorize | Out-String).Trim() -replace "`r", "" -split "`n"
foreach($line in $lines) {
$color = ''
foreach($pattern in $Colors.Keys){
if (!$SimpleMatch -and !$FullLine -and $line -match "([\s\S]*?)($pattern)([\s\S]*)") { $color = $Colors[$pattern] }
elseif (!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
}
if ($color -eq '') { Write-Host $line }
elseif ($FullLine -or $SimpleMatch) { Write-Host $line -ForegroundColor $color }
else {
Write-Host $Matches[1] -NoNewline
Write-Host $Matches[2] -NoNewline -ForegroundColor $color
Write-Host $Matches[3]
}
}
}
}
$linestocolor | Format-Color -Colors #{'6.1.7600' = 'Red'; '32-bit' = 'Green'}
# doesn't work...
# (Get-ChildItem | Format-Table -AutoSize) | Format-Color -Colors #{'sql' = 'Red'; '08/07/2016' = 'Green'}
# does work...
Format-Color -ToColorize (Get-ChildItem | Format-Table -AutoSize) -Colors #{'sql' = 'Red'; '08/07/2016' = 'Green'}
return
EDIT. to answer the OPs request
$Result = #()
foreach($server in Get-Content C:\PowerSQL\List.txt)
{
$Services=gwmi win32_service -computername $server | where {$_.Name -like ‘*SQL*’}
if(!(Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
{“Problem still exists in connecting to $server”}
else {
$services | ForEach {
If ($_)
{ $Result += New-Object PSObject -Property #{
HostName = $_.Systemname
ServiceDisplayName = $_.Displayname
ServiceName = $_.Name
StartMode = $_.Startmode
ServiceAccountName = $_.Startname
State = $_.State
Status = $_.Status
}
}
}
}
}
$Result | ConvertTo-HTML `
-Title "Services" `
-Body "<H2>The result of gwmi win32_service</H2> " `
-Property HostName,ServiceDisplayName,ServiceName,StartMode,ServiceAccountName,State,Status |
ForEach-Object {
if ($_ -like '<tr><td>*') {
switch ($_) {
{ $_ -like '*<td>Stopped</td>*' } {$color='red'}
{ $_ -like '*<td>Running</td>*' } {$color='green'}
Default {$color='white'}
}
$_.Replace('<tr>', "<tr bgcolor=`"$color`">")
} else {
$_
}
} | Set-Content C:\PowerSQL\service.htm -Force

try-catch bypassing a step

I have a script that tests connection to a list of servers, and if contactable, gets the status of a service, and puts the results into three variables, $Computer, $Ping (True/False), and $Service (Running or Stopped).
The output is in a hashtable but I can only get to show the servers that ARE contactable, and not the ones that cannot be contactable.
I have placed a try/catch in the $Ping block, as well as -ErrorAction Stop, so that it doesn't attempt to run the $Service script, and instead go to the next $Computer in the array. I think I am trying to do two things at once that are conflicting each other:
add the variables to the #Splat and
don't process any further.
There are actually many more remote registry queries in my script, which will be irrelevant if the $Computer cannot be contactable, but I have shortened it for this post.
Function Get-Ping {
$Servers = (gc "c:\temp\test.txt")
foreach ($Computer in $Servers) {
Write-Host
Write-Host "---------------------------------"
Write-Host "QUERYING $Computer"
Write-Host
Write-Host "Performing ping test..."
try {
$Ping = Test-Connection $Computer -Count 1 -ErrorAction Stop
} catch {
Write-Warning "Cannot Ping $Computer"
Write-Host "Trying next computer..."
Write-Host
continue
}
if ($Ping) {$Ping="$True"}
Write-Host $Computer "can be pinged"
$svcRRStopped = $false
if ($Computer -ne $env:COMPUTERNAME) {
Write-Host "Check RemoteRegistry status..."
}
$svcRR = Get-Service -ComputerName $Computer -Include RemoteRegistry
$SelectSplat = #{
Property = (
'Computer',
'Ping',
'Service'
)}
New-Object -TypeName PSObject -Property #{
Computer=$Computer
Ping=$Ping
Service=$svcRR.status
} | Select-Object #SelectSplat
}
}
$results = Get-Ping
$tableFragment = $results | Select 'Computer','Ping','Service'
$tableFragment
Don't make things more complicated than they need to be.
function Get-Ping {
Get-Content 'C:\temp\test.txt' | ForEach-Object {
$isAvailable = [bool](Test-Connection $_ -Count 1 -EA SilentlyContinue)
if ($isAvailable) {
$rreg = Get-Service -Computer $_ -Name RemoteRegistry |
Select-Object -Expand Status
} else {
$rreg = 'n/a'
}
New-Object -Type PSObject -Property #{
Computer = $_
Ping = $isAvailable
Service = $rreg
}
}
}
Get-Ping
You can simply use the -Quiet Parameter:
Test-Connection $_ -Count 1 -Quiet