LeVeilleur.net

Subscribe

PowerShell : Save Html, Body, ….

février 10, 2009 By: Christopher Keyaert Category: Non classé

Voici trois méthodes permettant de sauver une page web ou plus simplement du code html

# Internetexplorer.application
$obj=New-Object -ComObject internetexplorer.application
$obj.visible=$true
$obj.navigate2("http://www.google.com")
while($obj.ReadyState -ne 4){start-sleep -m 100}
$obj.Document.documentElement.outerHTML

# xml http
$url = "http://www.cnn.com"
$xHTTP = new-object -com msxml2.xmlhttp;
$xHTTP.open("GET",$url,$false);
$xHTTP.send();
$xHTTP.ResponseText; # returns the html doc

# net.webclient
$url="http://www.cnn.com"
$wc = new-object net.webclient
$html = $wc.DownloadString($url)

PowerShell : Merge files with a particular extension

février 05, 2009 By: Christopher Keyaert Category: powershell

Hello,

Voici une fonction powershell vous permettant de regrouper/merge de tous les fichiers portant une extension particulière.

$source="C:\"
$extenstion ="*.csv"
$outputFile = "MyMergeFile.csv"
$blockTerminator = "ENDOFFILE"

function mergeFile ($source, $extenstion, $outputFile, $blockTerminator)
{

[System.IO.DirectoryInfo]$directoryInfo = New-Object System.IO.DirectoryInfo($source);
$rgFiles = $directoryInfo.GetFiles($extenstion);
$builder = New-Object System.Text.StringBuilder;

foreach ($fileInfo in $rgFiles)
	{
	[System.IO.FileStream]$fReader = $fileInfo.OpenRead();
	if (-not ($fileInfo -eq $null))
		{
		write $fileInfo.Name;
		$reader = New-Object System.IO.StreamReader($fReader);
		$builder.AppendLine($reader.ReadToEnd());
		$builder.AppendLine($blockTerminator);
		}
	}
if (-NOT $source.EndsWith('\'))
	{
 	$source = $source + '\';
	}

$outputFile = $source + $outputFile;
[System.IO.FileStream]$fWriter = New-Object System.IO.FileStream($outputFile, [System.IO.FileMode]::OpenOrCreate);
$writer = New-Object System.IO.StreamWriter($fWriter);
$writer.Write($builder.ToString());
$writer.Flush();
$writer.Close();
}

mergeFile $source $extenstion $outputFile $blockTerminator

PowerShell : Control Excel

février 03, 2009 By: Christopher Keyaert Category: powershell

Voici quelques bouts de code pour controler Microsoft Excel à partir de PowerShell

#Creation of the xls Sheet

$a = New-Object -comobject Excel.Application
$a.visible = $True

$MyWorkBook = $a.Workbooks.Add()
$MyWorksheets = $MyWorkBook.Worksheets.Item(1)
$i=1;

$MyWorksheets.Cells.Item(1,$i++) = "Server Name"
$MyWorksheets.Cells.Item(1,$i++) = "Host"
$MyWorksheets.Cells.Item(1,$i++) = "Cluster"

#Excel Sheet Format
$d = $MyWorksheets.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True

$MyWorksheets.Cells.Item($intRow,$i++) = "OK"
$MyWorksheets.Cells.Item($intRow,$i-1).Interior.ColorIndex = 4

#Save the XLS page in CSV
$FileFormat=[Microsoft.Office.Interop.Excel.xlFileFormat]::xlCsv
if(test-path "$FileLocation\BocadaExport.csv"){Remove-Item "$FileLocation\BocadaExport.csv"}
$MyWorksheets.SaveAs("$FileLocation\BocadaExport.csv",$FileFormat)

#Quit XLS
$excel.displayalerts = $False
$excel.quit()

PowerShell : Control IE

février 03, 2009 By: Christopher Keyaert Category: powershell

Voici quelques bouts de code PowerShell permettant le control d’Internet Explorer

#Rune IE
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate('http://mypage.com')

#Authentication on the web page
while($ie.busy) {start-sleep 1}
if ($ie.document.getElementByID("FormUsername") -ne $null)
    {
    $ie.document.getElementByID("FormUsername").value = "myLogin"
    $ie.document.getElementByID("FormPassword").value = "MyPwd"

    #Click on the button for logon
    $forms = @($ie.Document.forms | where {$_.action -match "VerifPwd.htm"})
    $forms[0].submit()
    while($ie.busy) {start-sleep 1}
    }

$ie.navigate('http://mypage.com/page2.html')
while($ie.busy) {start-sleep 1} 

#Exit IE
$ie.quit()

PowerShell : Encrypt Password in text file

février 03, 2009 By: Christopher Keyaert Category: powershell

Hello tout le monde,

Voici un petit bout de code PowerShell vous permettant de crypter les mots de passes que vous utilisez dans vos scripts.

read-host -prompt "Enter password to be encrypted in file.pwdt " -assecurestring | convertfrom-securestring | out-file file.pwd

$pass = cat file.pwd | convertto-securestring
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$pass_back = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($ptr)

write-output "password decrpyted is: $pass_back"