Skip to content

Instantly share code, notes, and snippets.

@WildDogOne
Last active December 28, 2015 01:09
Show Gist options
  • Save WildDogOne/7418141 to your computer and use it in GitHub Desktop.
Save WildDogOne/7418141 to your computer and use it in GitHub Desktop.
Collection of little powershell things
cls
function Get-Excuse
{
$url = 'http://pages.cs.wisc.edu/~ballard/bofh/bofhserver.pl'
$ProgressPreference = 'SilentlyContinue'
$page = Invoke-WebRequest -Uri $url -UseBasicParsing
$pattern = '<br><font size = "\+2">(.+)'
if ($page.Content -match $pattern)
{
return ($matches[1])
}
}
write-host "Excuse: "(Get-Excuse)
getmac.exe /S 127.0.0.1 /FO CSV | ConvertFrom-Csv
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
# Show an Open Folder Dialog and return the directory selected by the user.
function Read-FolderBrowserDialog([string]$Message, [string]$InitialDirectory)
{
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, 0, $InitialDirectory)
if ($folder) { return $folder.Self.Path } else { return '' }
}
Creating Colorful HTML Reports
All PowerShell versions
To turn results into colorful custom HTML reports, simply define three script blocks: one that writes the start of the HTML document, one that writes the end, and one that is processed for each object you want to list in the report.
Then, hand over these script blocks to ForEach-Object. It accepts a begin, a process, and an end script block.
Here is a sample script that illustrates this and creates a colorful service state report:
$path = "$env:temp\report.hta"
$beginning = {
@'
<html>
<head>
<title>Report</title>
<STYLE type="text/css">
h1 {font-family:SegoeUI, sans-serif; font-size:20}
th {font-family:SegoeUI, sans-serif; font-size:15}
td {font-family:Consolas, sans-serif; font-size:12}
</STYLE>
</head>
<image src="http://www.yourcompany.com/yourlogo.gif" />
<h1>System Report</h1>
<table>
<tr><th>Status</th><th>Name</th></tr>
'@
}
$process = {
$status = $_.Status
$name = $_.DisplayName
if ($status -eq 'Running')
{
'<tr>'
'<td bgcolor="#00FF00">{0}</td>' -f $status
'<td bgcolor="#00FF00">{0}</td>' -f $name
'</tr>'
}
else
{
'<tr>'
'<td bgcolor="#FF0000">{0}</td>' -f $status
'<td bgcolor="#FF0000">{0}</td>' -f $name
'</tr>'
}
}
$end = {
@'
</table>
</html>
</body>
'@
}
Get-Service |
ForEach-Object -Begin $beginning -Process $process -End $end |
Out-File -FilePath $path -Encoding utf8
Invoke-Item -Path $path
#Show which paths have modules
$env:PSModulePath -split ';'
#Add more paths with modules
$env:PSModulePath += ';g:\mypersonalmodules'
Padding Strings Left and Right
If you must make sure that a given string has a uniform width, then you can use .NET methods to pad the string appropriately:
$mytext = 'Test'
$paddedText = $mytext.PadLeft(15)
"Here is the text: '$paddedText'"
$paddedText = $mytext.PadRight(15)
"Here is the text: '$paddedText'"
This is the result:
Here is the text: ' Test'
Here is the text: 'Test '
You can even add a padding character yourself (if you do not want to pad with spaces):
PS> 'Hello'.PadLeft(20, '.')
...............Hello
PS> 'Hello'.PadRight(20, '_')
Hello_______________
This is a collection of little powershell things I liked :3
$Path = "$home\Desktop\testfile.txt"
'Test' | Out-File "$home\Desktop\testfile.txt"
notepad $Path
Get-Content -Path $Path -Tail 0 -Wait | Out-GridView -Title $Path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment