Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get $webclient.downloadstring to write to text file in Powershell

I'm not a programmer/scripter. I just need to get the following script to write to a file:

[CmdletBinding()]
param ()

# Create a web client object
$webClient = New-Object System.Net.WebClient


# Returns the public IP address
$webClient.DownloadString('http://myip.dnsomatic.com/')

I've tried out-file and export-csv but it write a blank file. I'm sure it's something simple...but having no knowledge makes it difficult for me.

like image 255
pace Avatar asked Feb 20 '12 16:02

pace


2 Answers

You could also use the DownloadFile method:

$webClient.DownloadFile('http://myip.dnsomatic.com/', 'c:\ip.txt')
like image 92
Shay Levy Avatar answered Nov 15 '22 06:11

Shay Levy


The add-content cmdlet should do what you want.

Assuming $webClient.DownloadString('http://myip.dnsomatic.com/') returns a string, try:

Add-Content -Path $filename -Value $webClient.DownloadString('http://myip.dnsomatic.com/')

Reference: http://technet.microsoft.com/en-us/library/dd347594.aspx

like image 35
Arcass Avatar answered Nov 15 '22 06:11

Arcass