As a side note, PowerShell will only run digitally signed scripts so you might need change that setting to create scripts of your own. You should probably just make an internal CA for signing scripts, though.
Another interesting thing is the transcript function. The transcript will record to text file all of the contents of the shell window. Whatever you type and whatever is reported to the screen will be recorded. I think that is very useful, so I've come up with a profile script that will start the transcript automatically. Of course, that's a lot of text files, so I have the script clean that up for me, too.
Here is my script, colored and highlighted by PowerGUI (an open source PowerShell tool). I may have to experiment with the formatting to get it work correctly in the blog screen.
[string]$TimeStamp= get-date -uformat "%Y-%m-%d at %H%M%S"
$MaxAge = New-TimeSpan -days 7
$LaunchTime = Get-Date
$MyDocsPath = Get-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
$TranscriptFolder = $MyDocsPath.Personal + "\WindowsPowerShell"
[string]$TranscriptPath = $MyDocsPath.Personal + "\WindowsPowerShell\" + $TimeStamp + " Transcript.txt"
[string]$LaunchPath = $MyDocsPath.Personal + "\Script Experiments"
start-transcript $TranscriptPath
get-childitem -Path $TranscriptFolder -Filter *.txt | where { ($LaunchTime - $_.CreationTime ) -gt $MaxAge } | Remove-Item
cd $LaunchPath
The $TimeStamp variable is a date time formatted YYYYMMDD at HHDDSS so that the transcript that gets created has a unique, useful name.
The $MaxAge is how long I want to keep the transcripts. Date comparisions in PowerShell work completely differently than VBScript so you have to create a new date-time object for comparisons. It took a lot of experiments to get that working the way I wanted it to.
I read the location of My Documents from registry. I picked the registry so that I can be sure to grab the path of a relocated My Documents folder. I haven't tested this with a network-relocated My Documents folder, yet. One thing about reading the registry from PowerShell, you grab the key (the folder), then you retrive each value as a property. So, you'll notice that I grab the "Shell Folders" as $MyDocsPath and then get the value for the entry "Personal" by requesting the $MyDocsPath.Personal value. That is definately different than VBScript.
The $LaunchPath variable is just where I happen to keep my scripts, you would need to change this or delete this to match your preferences.
The script than purges any *Transcript.txt files that are older than the max age variable, starts the transcript for the current session, and changes the current directory to what I put into the $LaunchPath variable.
Let me know what you think...
2 comments:
Matthew,
Preserving the PowerShell formatting in your blog posts is very easy:
1. Select the code in the PowerGUI Script Editor.
2. Click Edit / Copy As / HTML.
3. Paste the html code into your post and enclose into pre tags to have the layout maintained.
Hope that helps!
Dmitry
Actually, the part that I was going to have difficulty with was the blog system. The standard input dialog box treats every new line/carriage return as a BR tag. That means that the HTML has to be entered as one big string and without the whitespace I am used to.
Post a Comment