Remote PowerShell Configuration - powershell-remoting

I have successfully created a remote PowerShell session. I want to change the IdleTimeout in a remote powershell session. I have run the $pssession -IdleTimeout 18000 and receive the error it is looking for a cmdlet.

The variable $pssession is not a script, function or cmdlet so it does not take parameters. New-PSSessionOption has the -IdleTimeout parameter. Save the output of that to a variable and pass that to the New-PSSession -SessionOption parameter when the session is created.
Add whatever else you need to something like the following commands:
$a = New-PSSessionOption -IdleTimeout 18000
$pssession = New-PSSession -SessionOption $a

Related

Bash variable not working in http request/response body

#!/bin/bash
object=$1
tenant=$2
server=$(sshpass -p 'password' ssh -tt root#ipaddress "/opt/something/sh/mysql -A 'admin_site' -e 'select id from something_servers where tenantcode=$2' | grep -o '[0-9]*'")
http -b "http://ipaddress/?app=something&t=users&v=users&server=$server&apikey=apikey&action=something.$1.list"
Everything seems to work fine. The value of variable $server is "24" as it should be. But still, when I call upon it in the http request it simply won't work... I honestly have no clue. I tested the variable's value by echoing it and it's correct. But for some reason when using it in the request it doesn't.
Any ideas?
Your code creates a variable called object and you set its value to the value stored in a variable called 1.
It creates a variable called tenant and sets it to the value of what's stored in the variable called 2.
It then creates a variable called server and then calls a subshell and runs the routine between $().
In the subroutine:
You use single quotes around
-e 'select id from something_servers where tenantcode=$2'
But for variable interpolation to work, you would need to use double quotes
Maybe that is your issue?

when I run my TCL script is there a way I can add a command or a variable with my run command?

What I am trying to do is run my TCL script with a variable.
So for example lets say my script was called example.tcl.
I want to be able to run that script doing something like this:
tclsh example.tcl success
Then in my tcl script I will have:
set status = variable <---- this variable should be equal to "success"
puts $status
Is there anyway in TCL I can do something like that?
The arguments to tclsh after the script name are stored as a list in the global argv variable.
set status [lindex $argv 0]
puts "status = $status"
The length of the list is in argc, but nobody really uses that. The script is in argv0, not in argv; that's very convenient sometimes.

Call a Powershell script function from CMD with parameters

Maybe i need a pair of fresh eyes on that:
i use the following code in a .bat file in order to call a Powershell script which accepts 2 parameters.And i want to pass the same parameters in a function which is the Powershell script.The problem is that i cannot seperate the 2 parameters inside the function.Here is the code inside the .bat file :
#ECHO OFf
Set OUTFILE="C:\users\XXX\desktop\newchangepass\log.txt"
echo user='%1' pass='%2' >> %OUTFILE%
echo %1 %2
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -File "C:\ps\CallMeWithParams.ps1" %1 %2
That's what i give to the command line:
c:\PS>CallMeWithParams.bat Alpha Beta
and here is the code inside the Powershell script:
param ([Parameter (Mandatory)]$param1, [Parameter(Mandatory)]$param2 )
function foo{
param ($fusername,$fpassword)
write-output $fusername
}
foo($param1,$param2)
The output which i receive obviously in CMD is:
c:\PS>CallMeWithParams.bat Alpha Beta
Alpha Beta
Alpha
Beta
i should get only the first parameter....
The code inside the PS script is there for test reasons.I want to be sure that i get the two parameters and that i can seperate them in order to use the second one in an update statement.
Any suggestion would be greatly appreciated.
PS1: Yes the CMD arguments will be enclosed in double quotes
PS2: atm there is no control on variable types etc. I wanted to keep it simple for debugging reasons.

Using Expect, how do I extract certain value from a file and input into a variable of an Expect script

I have two files. One is called bgp_ip.log and the other is an expect script called bgp.sh
bgp_ip.log contains just one IP address which was input into the file by another script called getip.sh which is working fine.
Now I'm trying to figure out how to get bgp.sh to extract the IP address in bgp_ip.log and place it into a variable container. In this script context, the variable container is called $BGP_IP as shown in the script below:
The reason for having this variable so that the script below is able to execute the Router BGP commands based on the IP address obtained from bgp_ip.log
#!/usr/bin/expect
set username "testaccount"
set password "testaccount"
set hostname "R-KANSAS-01"
set BGP_IP "?????" <<<< I'm not sure how can I extract the IP from bgp_ip.log and place into this variable
spawn telnet "$hostname"
expect "login: " {
send "$username\n"
expect "Password: "
send "$password\n"
expect "> "
send "show bgp summary instance $BGP_IP\n"
log_file "R-KANSAN-01_temp.log"
log_user 1
expect "#"
send "exit\n"; exit 0
interact
}
Any help is appreciated..Thanks.
You need to read the file and get the data from it
set file_name "bgp_ip.log"
set fp [open $file_name "r"]
set BGP_IP [read $fp]
close $fp
The variable BGP_IP will hold the IP address. I am assuming that the file contains only the IP address and nothing else.
If not, then you need regexp to get it.
Just add this code before spawning telnet in your code.

Directing output to stdout in a function without the calling code seeing it

I wrote several Powershell scripts which deploy software for a client. I used Write-Host to output a lot of information so that the progress of the deploy can be watched and they call this from one of their deploy application using Start-Transcript to capture this output.
However, they also need to be able to call some of these scripts from another application which can only capture output from stdout. This means that Write-Host won't work there since it outputs only to the console or host and doesn't get directed to stdout (correct?)
My thought was that I could change the code to use Write-Out instead, except that this causes another problem. Since I use functions and since functions in Powershell "return" everything that goes to stdout to the caller that would likely screw up any of my code that retrieves output from a function.
Is there a way to direct output to stdout from a function without it going to the calling code as the output of the function itself? Here is an example of the problem:
function Test-Output ([int]$number) {
Write-Output "This is a string"
return $number
}
[int]$someNumber = Test-Output 10
$someNumber
If you run the code above you'll see an error because Powershell is trying to assign "This is a string" to the integer $someNumber. If you change the variable to a string then it will capture the full output of the function (This is a string 10) and assign it to the variable.
Thanks for any suggestions that you can give!
Function output and stdout are the same thing so the calling code is going to see anything output to the stdout stream. In this case I would suggest using the Write-Progress cmdlet to report progress to the end user and leave actual function output alone.
Try this.
function Add-Numbers {
param (
[double] $FirstNumber,
[double] $SecondNumber
)
Write-Host "Hello World"
return ($FirstNumber + $SecondNumber)
}
$result = Add-Numbers 1 2
#Write-Host "Result is $result"