Good, this is the code I'm using:
function escribe_log($dispositivo, $log, $comando){
$Fichero_Log = "C:\Modificacion_routers.log"
$texto = $dispositivo
$texto >> $Fichero_Log
$texto = $log
$texto >> $Fichero_Log
$texto = $comando
$texto >> $Fichero_Log
}
##PROGRAMA PRINCIPAL##
Import-Module SSH-Sessions
$ficheroIPS = "C:\ipsdispositivos.txt"
$ficheroComandos = "c:\comandos.txt"
$dispositivos = Get-Content $ficheroIPS
$comandos = Get-Content $ficheroComandos
foreach ($dispositivo in $dispositivos) {
New-SshSession -ComputerName $dispositivo -Username USUARIO -password PASSWORD
foreach ($comando in $comandos) {
$SshResults = Invoke-SshCommand -ComputerName $dispositivo -Command $comando
escribe_log $dispositivo $SshResults $comando
Start-Sleep -s 1
}
$comandos = Get-Content $ficheroComandos
}
Remove-sshSession -RemoveAll
Works perfectly to launch "sh" show commands, my problem is that when I launch a command automatically disconnects the session, then command is not possible to launch a "conf t" to enter configuration mode and then launch configuration commands.
Do you know if this library is possible to configure a router?
if that is not possible,
would they know me any alternative to configure the router using script?
SOLVED:
FUNCTION FOR CHANGES TO SSH ROUTER/SWITCH WITH POWERSHELL
Function set-SSH($devices, $ssh){
function ReadStream($reader)
{
$line = $reader.ReadLine();
while ($line -ne $null)
{
$line
$line = $reader.ReadLine()
}
}
function WriteStream($cmd, $writer, $stream)
{
$writer.WriteLine($cmd)
while ($stream.Length -eq 0)
{
start-sleep -milliseconds 1000
}
}
$stream = $ssh.CreateShellStream("dumb", 80, 24, 800, 600, 1024)
$reader = new-object System.IO.StreamReader($stream)
$writer = new-object System.IO.StreamWriter($stream)
$writer.AutoFlush = $true
while ($stream.Length -eq 0)
{
start-sleep -milliseconds 500
}
ReadStream $reader
foreach ($command in $commands) {
WriteStream $command $writer $stream
ReadStream $reader
Write-Host $reader
start-sleep -milliseconds 500
}
$stream.Dispose()
$ssh.Disconnect()
$ssh.Dispose()
}
There are three variables to initialize $commands, $ssh, $devices
#Example of file commands.txt...
#sh conf
#conf t
#int f0/1
#description Modify via powershell
#exit
#exit
#wr
#...
$fileCommands= ".\commands.txt"
$commands= Get-Content $fileCommands
#Example of file devices.csv...
#10.10.10.10;SWITCH01
#10.10.10.11;SWITCH02
#10.10.10.12;SWITCH03
#...
$devices = Import-Csv -path ".\devices.csv" -header 'ip','hostname' -delimiter ';'
$port = 22
$ssh = new-object Renci.SshNet.SshClient($devices[0].ip, $port, $user, $password)
Try{
$ssh.Connect()
Start-Sleep -s 1
set-SSH $commands $ssh
}catch{
}
Related
I'm new with powershell and i would like to use a loop to ping several Printers on my network.
My problem is : once i'm in the loop of pinging , i can't go out of the loop ...
I tried several things from google but without success ( start-stop , Timer ) . Does anybody have any idea?
Here is the code :
$BtnStartPingClicked = {
if ($LblFileSelectPing.Text -eq "*.txt") {
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OK
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageBody = "Please select a list of printer first"
$MessageTitle = "Error"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
Write-Host "Your choice is $Result"
}
else {
do {
$IPList = Get-Content ($LblFileSelectPing.Text)
$snmp = New-Object -ComObject olePrn.OleSNMP
$ping = New-Object System.Net.NetworkInformation.Ping
$i = 11
$j = 1
foreach ($Printer in $IPList) {
try {
$result = $ping.Send($Printer)
} catch {
$result = $null
}
if ($result.Status -eq 'Success') {
$((Get-Variable -name ("GBMachine"+$j+"Ping")).value).Visible = $True
$j++
test-Connection -ComputerName $Printer -Count 1 -Quiet
$printerip = $result.Address.ToString()
# OPEN SNMP CONNECTION TO PRINTER
$snmp.open($Printer, 'public', 2, 3000)
# MODEL
try {
$model = $snmp.Get('.1.3.6.1.2.1.25.3.2.1.3.1')
} catch {
$model = $null
}
# Serial
try {
$serial = $snmp.Get('.1.3.6.1.4.1.1602.1.2.1.8.1.3.1.1').toupper()
} catch {
$Dns = $null
}
# progress
$TBMonitoringPing.SelectionColor = "green"
$TBMonitoringPing.AppendText("$Printer is Pinging")
$TBMonitoringPing.AppendText("`n")
$mac = (arp -a $Printer | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}').Matches.Value
# OPEN SNMP CONNECTION TO PRINTER
$((Get-Variable -name ('LblMach' + $i)).value).Text = "IP : $Printerip"
$i++
$((Get-Variable -name ('LblMach' + $i)).value).Text = "Model : $Model"
$i++
$((Get-Variable -name ('LblMach' + $i)).value).Text = "MAC : $mac"
$i++
$((Get-Variable -name ('LblMach' + $i)).value).Text = "Serial : $serial"
$TBAnswerMachine.AppendText("$Model")
$TBAnswerMachine.AppendText("`n")
$TBAnswerMachine.AppendText("$Printer - $Serial")
$TBAnswerMachine.AppendText("`n")
$TBAnswerMachine.AppendText("$Mac")
$TBAnswerMachine.AppendText("`n")
$TBAnswerMachine.AppendText("`n")
Get-Content ($LblFileSelectPing.Text) | Where-Object {$_ -notmatch $Printer} | Set-Content ("C:\_canonsoftware\out.txt")
$i = $i+7
$snmp.Close()
Start-Sleep -milliseconds 1000 # Take a breather!
}
else {
$TBMonitoringPing.selectioncolor = "red"
$TBMonitoringPing.AppendText("$Printer not pinging")
$TBMonitoringPing.AppendText("`n")
Start-Sleep -milliseconds 1000 # Take a breather!
}
}
$LblFileSelectPing.Text = "C:\_canonsoftware\out.txt"
} until($infinity)
}
}
thanks for your answers...
1 - part of my object are indeed not declared in the code because they are in my other PS1 file....
2 - I do a do until infinity because i don't want to stop the code before i decide it...
3 - I didn't explain my problem correctly ( excuse my poor english ) ... i would like to be able to go out of the loop do until at the moment i click on a stop button ... but apprently the windows doens't respond while in the loop ... i have to stop the script with powershell ... which is annoying because i'd like to make an executable with it ... and not have to go out of my program ...
thank you for your ideas
I have created below function in Powershell to convert an .xlsx file into .csv ,the script is running in windows server 2012 machine.The script is getting complete without any error but it not doing the conversion.When same script I have tried in win 10 machine its working perfectly.Can you please confirm how to fix this issue.Is it due to server OS as excel is not installed in it ?
function xlsx_to_csv($xlinput,$csvout) {
$excel = New-Object -ComObject excel.application
$excel.DisplayAlerts = $false
$Workbook = $excel.Workbooks.Open("$xlinput")
$Workbook.SaveAs("$csvout",6)
$excel.Quit()
}
xlsx_to_csv $yest_xl_in $yest_csv_in
$yest_xl_in and $yest_csv_in are the xlsx and csv file locations.
Here is a more elegant function to convert an XLSX file into a csv. I have been using it for quite some time now and it has never let me down, yet! :)
function Get-ExcelData {
[CmdletBinding(DefaultParameterSetName='Worksheet')]
Param(
[Parameter(Mandatory=$true, Position=0)]
[String] $Path,
[Parameter(Position=1, ParameterSetName='Worksheet')]
[String] $WorksheetName = 'Sheet1',
[Parameter(Position=1, ParameterSetName='Query')]
[String] $Query = 'SELECT * FROM [Sheet1$]'
)
switch ($pscmdlet.ParameterSetName) {
'Worksheet' {
$Query = 'SELECT * FROM [{0}$]' -f $WorksheetName
break
}
'Query' {
# Make sure the query is in the correct syntax (e.g. 'SELECT * FROM [SheetName$]')
$Pattern = '.*from\b\s*(?<Table>\w+).*'
if($Query -match $Pattern) {
$Query = $Query -replace $Matches.Table, ('[{0}$]' -f $Matches.Table)
}
}
}
# Create the scriptblock to run in a job
$JobCode = {
Param($Path, $Query)
# Check if the file is XLS or XLSX
if ((Get-Item -Path $Path).Extension -eq 'xls') {
$Provider = 'Microsoft.Jet.OLEDB.4.0'
$ExtendedProperties = 'Excel 8.0;HDR=YES;IMEX=1'
} else {
$Provider = 'Microsoft.ACE.OLEDB.12.0'
$ExtendedProperties = 'Excel 12.0;HDR=YES'
}
# Build the connection string and connection object
$ConnectionString = 'Provider={0};Data Source={1};Extended Properties="{2}"' -f $Provider, $Path, $ExtendedProperties
$Connection = New-Object System.Data.OleDb.OleDbConnection $ConnectionString
try {
# Open the connection to the file, and fill the datatable
$Connection.Open()
$Adapter = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter $Query, $Connection
$DataTable = New-Object System.Data.DataTable
$Adapter.Fill($DataTable) | Out-Null
}
catch {
# something went wrong ??
Write-Error $_.Exception.Message
}
finally {
# Close the connection
if ($Connection.State -eq 'Open') {
$Connection.Close()
}
}
# Return the results as an array
return ,$DataTable
}
# Run the code in a 32bit job, since the provider is 32bit only
$job = Start-Job $JobCode -RunAs32 -ArgumentList $Path, $Query
$job | Wait-Job | Receive-Job
Remove-Job $job
}
Now to get your csv file, you can simply do -
$csvfile = Get-ExcelData -Path 'PathToYourExcelFile\YourExcelFile.xlsx'
$csvfile | Export-Csv -path $env:USERPROFILE\Desktop\CsvFileName.csv -NoTypeInformation #Save the csv file on your Desktop
I use this powershell script for getting output stream of telnet request.
Function Get-Telnet {
Param (
[Parameter(ValueFromPipeline=$true)]
[string[]]$Commands = #(),
[string]$RemoteHost = "0.0.0.0",
[string]$Port = "23",
[int]$WaitTime = 1000,
[string]$OutputPath = "C:\temp\telnet_output.log"
)
#Attach to the remote device, setup streaming requirements
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
if ($Socket) {
$Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
#Now start issuing the commands
foreach ($Command in $Commands) {
$Writer.WriteLine($Command)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
}
#All commands issued, but since the last command is usually going to be
#the longest let's wait a little longer for it to finish
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = ""
#Save all the results
while ($Stream.DataAvailable) {
$Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
} else {
$Result = "Unable to connect to host: $($RemoteHost):$Port"
}
#Done, now save the results to a file
#$Result | Out-File $OutputPath
$Result
}
This works for a Cisco switch and a telnet server, but for the Alcatel OmniPCX and the HPUX the stream reading is empty.
Here is my Powershell code that when i execute it, it will continually loop while waking up my internal sites
$urlFile = "C:\Users\lfouche\Desktop\url\url.txt"
foreach($site in Get-Content $urlFile){
function WakeUp([string] $url)
{
Write-Host "Waking up $url ..." -NoNewLine
$client = new-object system.net.WebClient
$client.UseDefaultCredentials = $true
$null = $client.OpenRead($url)
$client.Dispose()
Write-Host " Ok"
}
#(
Get-Content $urlFile
) | % { WakeUp $_ }
}
Well, your script can generally be improved, however I think the error is that you already iterate over Get-Content $urlFile within your foreach condition and also in the loop. Try this:
# define the wake up function once
function WakeUp
{
Param
(
[string] $url
)
Write-Host "Waking up $url ..." -NoNewLine
$client = new-object system.net.WebClient
$client.UseDefaultCredentials = $true
$null = $client.OpenRead($url)
$client.Dispose()
Write-Host " Ok"
}
$urlFile = "C:\Users\lfouche\Desktop\url\url.txt"
$sites = Get-Content $urlFile
foreach($site in $sites)
{
# call wake up for each site
WakeUp $site
}
I'm new to powershell. I read some lines on www.powershell.com. Now I need your help to solve a problem. I want to read the UUID from clients in the Network. Therefore I created a document "pcs.txt" where all PCs are stored.
$pc = Get-Content pcs.txt #Read content of file
$cred = Get-Credential “domain\user”
for ($i=0; $i -lt $pc.length; $i++) {
$Result=test-connection -ComputerName $pc[$i] -Count 1 -Quiet
If ($Result -eq 'True')
{
$uuid = (Get-WmiObject Win32_ComputerSystemProduct -ComputerName $pc[$i] -Credential $cred).UUID
$Ausgabe=$pc[$i] + ';'+$uuid
$Ausgabe
}
else
{
$Ausgabe=$pc[$i] + '; UUID nicht erhalten'
$Ausgabe
}
}
First I test if the ping works. When the ping works I try to get the uuid.
Sometimes I don't get the uuid even if the ping worked. So I would like to code a timeout, which say -> go to next pc when you don't have the uuid after 2 seconds.
Can you help me please?
Alas, there is no timeout parameter for Get-WmiObject commandlet. There is a feature request in MS Connect, but it is from 2011 and still open.
A workaround, which I haven't tested is available by using System.Management. I'll copy-and-paste it here in case the link goes dead. (And I hate SO answers that only contain links to resouces that may or may not exist...)
Function Get-WmiCustom([string]$computername,[string]$namespace,[string]$class,[int]$timeout=15){
$ConnectionOptions = new-object System.Management.ConnectionOptions
$EnumerationOptions = new-object System.Management.EnumerationOptions
$timeoutseconds = new-timespan -seconds $timeout
$EnumerationOptions.set_timeout($timeoutseconds)
$assembledpath = "\\" + $computername + "\" + $namespace
#write-host $assembledpath -foregroundcolor yellow
$Scope = new-object System.Management.ManagementScope $assembledpath, $ConnectionOptions
$Scope.Connect()
$querystring = "SELECT * FROM " + $class
#write-host $querystring
$query = new-object System.Management.ObjectQuery $querystring
$searcher = new-object System.Management.ManagementObjectSearcher
$searcher.set_options($EnumerationOptions)
$searcher.Query = $querystring
$searcher.Scope = $Scope
trap { $_ } $result = $searcher.get()
return $result
}
I found a good workaround!
http://theolddogscriptingblog.wordpress.com/2012/05/11/wmi-hangs-and-how-to-avoid-them/
Here my working code:
$pc = Get-Content pcs.txt #FILE FROM THE HARDDISK
$cred = Get-Credential “DOMAIN\USER” #
for ($i=0; $i -lt $pc.length; $i++)
{
$Result=test-connection -ComputerName $pc[$i] -Count 1 -Quiet
If ($Result -eq 'True')
{
$WMIJob = Get-WmiObject Win32_ComputerSystemProduct -ComputerName $pc[$i] -Credential $cred -AsJob
$Timeout=Wait-Job -ID $WMIJob.ID -Timeout 1 # the Job times out after 1 seconds.
$uuid = Receive-Job $WMIJob.ID
if ($uuid -ne $null)
{
$Wert =$uuid.UUID
$Ausgabe=$pc[$i] + ';'+$Wert
$Ausgabe
}
else
{
<#$b = $error | select Exception
$E = $b -split (:)
$x = $E[1]
$Error.Clear() #>
$Ausgabe=$pc[$i] + '; got no uuid'
$Ausgabe
}
}
else
{
$Ausgabe='PC not reached through ping.'
$Ausgabe
}
}
I hope I can help somebody with that