IT tutorials
 
Windows
 

Windows Server 2008 : Creating and Running a PowerShell Script - Testing for the Existence of a File, Creating Output as HTML

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/29/2013 8:02:48 PM

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 LinesComments
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 HTMLComments
$computer = get-content
env:computername

This retrieves the name of the computer and stores it in the $computer variable.
get-service |

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.

Figure 1. Display running services in an HTML file

 
Others
 
- Windows Server 2008 : Creating and Running a PowerShell Script - Running PowerShell Scripts, Logging Processes with a get-process Script
- Windows Server 2008 : Creating and Running a PowerShell Script - Creating and Modifying the Global PowerShell Profile
- Windows Server 2008 : Creating and Running a PowerShell Script - Creating a PowerShell Profile
- Windows Server 2008 : Creating and Running a PowerShell Script - Setting the Security Context
- Windows 8 : Cloud Connections - Office 2013
- Windows 8 : Cloud Connections - SkyDrive (part 3) - To access SkyDrive from a browser
- Windows 8 : Cloud Connections - SkyDrive (part 2) - To open SkyDrive inside Windows Explorer
- Windows 8 : Cloud Connections - SkyDrive (part 1) - To add a file to the tile-based SkyDrive app
- Windows 8 : Cloud Connections - Windows Essentials (part 2) - To install Windows Essentials
- Windows 8 : Cloud Connections - Windows Essentials (part 1)
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us