Powershell: Create a tempfile with Powershell

Powershell Header

Sometimes you need to save some data in to a tempfile. And instead of creating a ownen file for this you could use the .NET function to create a really tempfile in the tempfile directory.

This is the how you can create a new tempfile:

$tempFile = [System.IO.Path]::GetTempFileName()

And now you can parse data into this file.

Powershell and RSS Feeds

Powershell Header

This is a Quick Powershell note how you can use Powershell to read RSS feeds. This is pretty simple you can use the .NET class WebClient.

$rssFeed = [xml](New-Object System.Net.WebClient).DownloadString('http://www.thomasmaurer.ch/feed/')
$rssFeed.rss.channel.item | Select-Object title -First 5

 

Error: Unable to get the private bytes memory limit for the W3WP process

Error

==================
Event Type: Error
Event Source: ASP.NET 2.0.50727.0
Event Category: None
Event ID: 1093
Date: 13.01.2011
Time: 11:11:50
User: N/A
Computer: Server01

Description:

Unable to get the private bytes memory limit for the W3WP process. The ASP.NET cache will be unable to limit its memory use, which may lead to a process restart. Error: 0×80070005

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
==================

Environment

  • Windows Server 2003
  • IIS 6
  • .NET 2.0 and 1.1

Summary

This Error is caused by the a know issue with permissions of the IIS metabase. The metabse ACL’s do not include the IIS_WPG group on the W3SVC/AppPools part in the metabase.

Solution

  • Download metaacl.vbs
  • Run the following command:
    cscript metaacl.vbs IIS://Localhost/W3SVC/AppPools IIS_WPG RE

More Information

Powershell FTP upload and download

Powershell Header

A friend asked me if Powershell can do simple file up and downloads. My answer was, yes of course, very easy. So this is a post with a little information about how you can do a FTP Upload or a FTP Download using Powershell. To be clear, of course you can do much more. But this post should show you the basic function of FTP transfers in Powershell based on the .NET Framework.

Download File:

# Config
$Unsername = "FTPUSER"
$Password = "P@assw0rd"
$LocalFile = "C:\Temp\file.zip"
$RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"

# Create FTP Rquest Object
$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Read the File for Upload
$FileContent = gc -en byte $LocalFile
$FTPRequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
$Run = $FTPRequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
# Cleanup
$Run.Close()
$Run.Dispose()

Upload File:

# Config
$Unsername = "FTPUSER"
$Password = "P@assw0rd"
$LocalFile = "C:\Temp\file.zip"
$RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"

# Create a FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
	do {
		$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
		$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
	}
	while ($ReadLength -ne 0)

How to download files with Powershell

Powershell HeaderThis should show you how you can download a file with Powershell. This is not a script or function you should use. It just is the the easyiest way to download a file with Powershell. If I have enough time I will create a function for downloading files.

$Url = "http://www.thomasmaurer.ch/ps.txt"
$Path = "C:\temp\ps.txt"
$Username = ""
$Password = ""

$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
$WebClient.DownloadFile( $url, $path )

Random Numbers in Powershell

Powershell Header

In one of my last scripts I needed random numbers in Powershell. Now this Blog post should show you how you can generate random numbers in Powershell very simple.

To create random numbers in Powershell you can use the .NET Framework class “System.Random”. First you have to create a new object.

[Object]$Random = New-Object  System.Random

Lets checkout the $Random object:

$Random | Get-Member
   TypeName: System.Random

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
Next        Method     int Next(), int Next(int minValue, int maxValue), int Next(int maxValue)
NextBytes   Method     System.Void NextBytes(byte[] buffer)
NextDouble  Method     double NextDouble()
ToString    Method     string ToString()

Now with this object you can now create random numbers very simple:

Create a random double number:

$Random.NextDouble()

Create a random int number:

$Random.Next()

Create a random number between 1 or 10:

$Random.Next(1,11)

Create a random number between 12 or 25:

$Random.Next(12,26)

Create a random number 1 or 0:

$Random.Next(0,2)