1. Testing for the Existence of a File
You can use the following script snippets to test
for the existence of a file and either delete or rename the file
depending on which code snippet you choose. This has multiple uses. For
example, if you want to create a listing of computers in the domain,
but you don’t want the new listing appended to the current file, you
can either delete or rename the existing file. Similarly, if you have a
script that writes data to a file for logging, you can use one of these
scripts to handle existing files.
Script Lines | Comments |
---|
Delete a file if it exists.
If (test-path c:\data\ computerlist.txt) { remove-item c:\data\ computerlist.txt }
| The test-path cmdlet checks for the existence of the file. If it exists, it returns true and the lines within the curly brackets execute.
The example checks for C:\data\computerlist.txt and deletes if it exists with the remove-item cmdlet. |
Rename a file if it exists.
If (test-path c:\data\ computerlist.txt) { $dt = (get-date). tostring('MMM_dd_yyyy_hh_mm') $newname = "computerlist_" + $dt + ".txt" rename-item -path c:\data\ computerlist.txt -newname $newname }
| Alternatively, you can archive the item by renaming it with the rename-item cmdlet.
Note
Use this code instead of the remove-item code in the preview row if you want to archive the item.
This code renames the file by appending the date and time to the file name. |
Copy a file if it doesn’t exist.
If (test-path c:\gpo_adm\ grouppolicy-server.admx) { #do nothing } else { $objnet = $(new-object -com wscript.network) $objnet.mapnetworkdrive ("x:", "\\dc1\admx") copy-item x:\grouppolicy-server.admx -destination c:\gpo_adm $objnet. removenetworkdrive("x:") }
| This code checks for the existence of a file and copies it from a share if it doesn’t exist.
If the file exists, the comment #do nothing is ignored. If the file doesn’t exist, it’s copied from a network share.
Because the copy-item cmdlet can’t copy from a share directly, the share must first be created using the new-object cmdlet from the wscript.network object and the mapnetworkdrive method. The network object is stored in the $objnet variable.
Note
You cannot have any spaces between the mapnetworkdrive method and the first parentheses.
With the drive mapped, the copy-item can copy the file from the mapped drive to the local destination.
Last, the removenetworkdrive method is used to remove the x: mapped network drive.
Note
You cannot have any spaces between the removenetworkdrive method and the first parentheses.
|
2. Creating Output as HTML
You can use the following script to capture data and
send the output to an HTML file. Although this script captures the
output of a get-service command, you can use it for other commands as well.
$computer = get-content env:computername
get-service |
sort-object -property status -descending |
convertto-html -title "Running Services" -body "<h1>Running
Services on $computer</h1> " -property DisplayName, Name, Status |
foreach {
if($_ -like "*<td>Running</td>*")
{$_ -replace "<tr>", "<tr bgcolor=ffffcc >"}
elseif($_ -like "*<td>Stopped</td>*")
{$_ -replace "<tr>", "<tr bgcolor=ffccff >"}
else{$_}
} > c:\scripts\get-service.html
c:\scripts\get-service.html
The following table explains the lines in this script.
Script Lines to Send Output to HTML | Comments |
---|
$computer = get-content env:computername
| This retrieves the name of the computer and stores it in the $computer variable. |
| The rest of the script (except the last line) is a single line with several pipes. The first cmdlet is the get-service cmdlet ending with the pipe symbol (|). |
Sort-object -property status -descending |
| Next,
the output is sorted based on the status (running or stopped) in
descending order so that running services are listed first. It finishes
with a pipe. |
convertto-html -title "Running Services" -body "<h1>Running Services on $computer</h1> " -property DisplayName, Name, Status |
| The convertto-html cmdlet converts the output to HTML with some HTML codes. The title is in the title bar of the web browser. The <h1> code provides a heading at the top of the page with the $computer variable displaying the name. Last, the -property switch identifies the three columns to include and lists them as column headings.
Note
The column headings are not case sensitive but are displayed in the
same case you enter them. If you use this script for something other
than the get-service applet, you need to modify the columns in the -property list.
|
foreach { if($_ -like "*<td>Running</td>*") {$_ -replace "<tr>", "<tr bgcolor=ffffcc >"} elseif($_ -like "*<td>Stopped</td>*") {$_ -replace "<tr>", "<tr bgcolor=ffccff >"} else{$_} } > c:\scripts\get- service.html
| The foreach
command loops through the page to change the background colors of the
rows. If the service is running, the row is changed to a light yellow
color. If the service is not running, the row is changed to a reddish
color.
The redirector (>) then sends the output to the c:\scripts\get-service.html file. |
c:\democode\ get-service.html
| Last, the file is opened. It looks similar to Figure 1. Because it is an HTML file, it automatically launches the default web browser. |