Tag: .NET

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 = <pre lang="xml">(New-Object System.Net.WebClient).DownloadString('https://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: 0x80070005

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



Windows Server FTP

PowerShell FTP upload and download

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 with PowerShell and FTP. But this post should show you the basic function of FTP transfers in Powershell based on the .NET Framework. With that here is how PowerShell FTP works:

FTP Upload file using PowerShell

This is how you can upload a file using FTP with PowerShell.

# Config
$Username = "FTPUSER"
$Password = "[email protected]"
$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()

FTP download file using PowerShell

And this is how you can download files from an FTP server using PowerShell.

# Config
$Username = "FTPUSER"
$Password = "[email protected]"
$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)

So let me know how FTP file transfers with PowerShell are working for you. Scenarios I have used this are simple automation tasks where we needed to upload or download some files.



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 = "https://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)


Genotec released Shared Hosting with .NET Framework 4

Last Monday we added support for the .NET Framework 4 to our Windows Shared Hosting Server at Genotec. Today the official Press release was published.

We also added support for:

  • .NET Framework 4
  • ASP.NET MVC 2.0
  • Silverlight 4
  • New Versions of Persits AspMail, AspPDF, AspJPEG

Genotec: .Net Freamwork 4 for Webhosting (German)

Der in Allschwil/Basel ansässige Internet Service Provider Genotec AG lanciert mit .NET Framework 4 die neueste Version des Microsoft Entwickler-Frameworks.

Microsoft .NetMit dem Ziel stets neue Features und aktuelle Produkte zu integrieren, hat Genotec das .NET Framework 4 von Microsoft lanciert. Ab sofort steht allen Windows Webhosting-Kunden der Genotec dieses Angebot zur Verfügung.

«Die neue Version von .NET lässt Projekte im Windows-Umfeld noch einfacher und mit vielen neuen Features erstellen. Unsere Kunden werden grosse Freude daran haben.» so Thomas Betz, Entwickler bei Genotec. Die neue Version mit vielen Features überzeugt auf der ganzen Linie. Zu den wichtigsten Neuerungen zählen unter anderem Codeverträge, eine DLR (Dynamic Language Runtime), neue Diagnose und Leistungstools sowie eine verbesserte automatische Speicherbereinigung. Die komplette Abwärtskompatibilität rundet den sehr guten Eindruck des .NET Framework 4 ab.

Verfügbar ist das .NET 4.0 bereits ab dem kleinsten Angebot für CHF 9.90 pro Monat. Mit dem Shared Hosting Entry erhalten Kunden ein Webhosting mit bis zu 10 Domains, komplettes Mailhosting inkl. Webmail, tägliches Backup, Statistiken sowie der kompletten Integration ins Hosting Control Center von Genotec und weiteren Features.

Über Genotec AG:
Der Schweizer Internet Service Provider Genotec AG wurde 2001 gegründet. Das Unternehmen beschäftigt 36 Mitarbeitende und betreut in den eigenen Rechenzentren in Zürich, Basel, Bern und Allschwil über 50’000 Domain- und rund 500 Serverkunden. Genotec ist ein BAKOM-registrierter Provider und als «Microsoft Gold Certified Partner» zertifiziert. Genotec wurde von der simsa mit dem Gütesiegel «Swiss Quality Hosting» ausgezeichnet und ist providerliste.ch geprüft.

Seit 2008 ist Genotec neu bei Computerworld in den Top-500 der stärksten IT-Firmen der Schweiz gelistet.

Weitere Informationen zur Genotec: www.genotec.ch

Pascal Leu, Product Manager
Genotec AG
Binningerstrasse 95
CH – 4123 Allschwil
Tel. +41 (0)848 321 123

E-Mail: [email protected]