Monthly Archive for June, 2007

Run Silent PowerShell Tasks

I was looking online for a way to run a .ps1 file silently from my workstation and found this great post by Jeffery Hicks about how to use WScript to wrapper a powershell command, making use of the ability to run things with a hidden window. For some reason this is the only way I’ve found to get WPS to run completely silently. Not even the -nologo -noninteractive triggers works as well as this simple script. All you have to do is take the code below and place it into a .vbs file.

PSWrapper.vbs

Dim objShell,objFSO,objFile

Set objShell=CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")

'enter the path for your PowerShell Script
strPath="C:\bri\bkupspace.ps1"

'verify file exists
If objFSO.FileExists(strPath) Then
'return short path name
set objFile=objFSO.GetFile(strPath)
strCMD="powershell -nologo -command " & Chr(34) & "&{" &_
objFile.ShortPath & "}" & Chr(34)
'Uncomment next line for debugging
'WScript.Echo strCMD

'use 0 to hide window
objShell.Run strCMD,0

Else

'Display error message
WScript.Echo "Failed to find " & strPath
WScript.Quit

End If

Then, edit the file you just created and replace the line that looks like the one below to contain your .ps1 file.

strPath=”e:\documents and settings\jhicks\my documents\scripts\posh\Get-DiskSize.ps1″

Now you can open up the windows task scheduler (Start, Run, ‘Tasks’) and make a task to the VBS file with no triggers required!.
Thanks again Jeffery!

A few New Useful Tools in Windows Vista

Checking around online I found an interesting post at computerbits.wordpress.com about the new utilities within Vista. I really liked a few:

cmdkey Creates, displays, and deletes stored user names and passwords.
netcfg WinPE Network Installer
ocsetup Windows Optional Component Setup
pnpunattend AuditSystem, Unattend online driver install
robocopy Robust File Copy for Windows (it’s now included in windows)
takeown This tool allows an administrator to recover access to a file that was denied by re-assigning file ownership.
wbadmin Backup command-line tool
wceutil Windows Event Collector Utility
wevtutil Windows Events Command Line Utility.
winrs Windows Remote Shell
winrm Windows Remote Management

Save PowerShell Credentials Permanently

I just found a post on geekswithblogs.net that shows how to store your password in a text file, then call it back as a secure credentials object. This is extremely useful for putting in your PowerShell profile if you use multiple passwords or admin multiple domains. Keep in mind where he has ‘test’ in his script, should be replaced with your desired account name.

Here’s the code to store the credential:

read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt

And here is the code to call it back and store it as a variable (this is where you replace the word ‘test’). Change $mycred to whatever you want the credential variable to be.

$pass = cat C:\securestring.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist “test”,$pass

Here is example useage if you left the credential as $mycred:

gwmi win32_computersystem -computer somecomputer -cred $mycred

Change *.ps1 Icons to PowerShell Icons

ps1-icons.JPG
(click to enlarge)

This is kind of a standard windows thing, but I thought I would share anyway because most people probably didn’t know they could do this and well, it’s just plain handy. Basically were chaning files that have a .ps1 extension to have a powershell icon instead of a windows text document icon.

Step 1
Open the folder where you want the icons to appear differently and make sure you have a few in there.

Step 2
Click the Tools menu, then choose ‘Folder Options’. Click the ‘File Types’ tab after that.

Step 3
Click on the PS1 file extension listed and click Advanced on the bottom of the window.

Step 4
Click ‘Change Icon’ and paste this path (without quotes):

“%SystemRoot%\system32\windowspowershell\v1.0\powershell.exe”

Step 5
Click ‘Ok’ then click ‘Ok’ on the next window.

Step 6
Done!