Running fastlane in Powershell and try to get environment variable set by fastlane.
I have the following code, that I am trying to use to get the variable, but the FASTLANE_SESSION is empty after the loop - that is why I try to set an env myself in the loop:
fastlane spaceauth -u user#domain.com 2>&1 | ForEach-Object {
Write-Host $env:FASTLANE_SESSION
return $_
}
Any ideas for solving this?
If the last line of the fastlane output is empty, e.g., line feed(s), that's what you get. Don't have fastlane, so tested with java instead. First test for [string] like your example:
java -version 2>&1 | foreach { if ($_ -is [string]) { return $_; } } | select -Last 1;
no output.
Second test for non-whitespace characters:
java -version 2>&1 | where { $_ -match '\S+' } | select -Last 1;
outputs the currently installed version.
For your specific example something like this should work:
$OUTPUT = fastlane spaceauth -u email#adomain.com 2>&1 |
where { $_ -match '\S+' } |
select -Last 1;
Related
I have this script:
$params = '/r', '/f',
'/t', '0',
'/d', 'p:0:0',
'/c', 'PlannedRestart'
$servers | ForEach-Object {
$output = & shutdown.exe /m "\\${_}" #params 2>&1
if ($LastExitCode -eq 0) {
"{0}`tRestarted" -f $_
} else {
"{0}`tRestart failed:`t{1}" -f $_, $output
}
} | Set-Content '.\RestartServers_LOG.txt'
it prints like this when it fails:
server1 Restart failed:server1:error
i want it to print:
server1 Restart failed:error
If the hostname always appears at the beginning of the captured output you can remove it with a simple replacement, e.g. like this:
"{0}`tRestart failed:`t{1}" -f $_, ($output -replace "^${_}:")
If it can appear at different locations in the output string you need to provide a more complete output example.
I have following content in a configuration file (sample.cfg),
Time_Zone_Variance(Mins):300
Alert_Interval(Mins):2
Server:10.0.0.9
Port:1840
I'm trying to store an each values after the : by using split in PowerShell. but i'm not able to produce require output.
Can someone tell me how to use PowerShell split for the above problem ?
You can read the contents of the file using Get-Content, then pipe each line through ForEach-Object, then use the split command on each line, taking the second item in the array as follows:
$filename = "sample.cfg"
Get-Content $filename | ForEach-Object {
$_.split(":")[1]
}
Output
300
2
10.0.0.9
1840
Update
I prefer the approach by #AnsgarWiechers, but if you really need specifically named values you could create a hashtable and replace the name with the value:
$configValues = #{
hour = "Time_Zone_Variance(Mins)"
min = "Alert_Interval(Mins)"
server = "Server"
port = "Port"
}
Get-Content $filename | ForEach-Object {
# Courtesy of Ansgar Wiechers
$key, $value = $_ -split ':', 2
foreach($configValuesKey in $($configValues.keys)) {
if ($configValues[$configValuesKey] -eq $key)
{
$configValues[$configValuesKey] = $value
}
}
}
write-host "`nAll Values:"
$configValues
write-host "`nIndividual value:"
$configValues.port
Output
All Values:
Name Value
---- -----
port 1840
min 2
server 10.0.0.9
hour 300
Individual value:
1840
How's this?
function cut {
param(
[Parameter(ValueFromPipeline=$True)] [string]$inputobject,
[string]$delimiter='\s+',
[string[]]$field
)
process {
if ($field -eq $null) { $inputobject -split $delimiter } else {
($inputobject -split $delimiter)[$field] }
}
}
PS C:\> 'hi:there' | cut -f 0 -d :
hi
PS C:\> 'hi:there' | cut -f 1 -d :
there
PS C:\> 'hi:there' | cut -f 0,1 -d :
hi
there
PS C:\> 'hi:::there' | cut -f 0 -d :+
hi
PS C:\> 'hi there' | cut
hi
there
For a more succint syntax, this will also do the trick:
((Get-Content "your-file.txt") -Split ":")[1]
So the trick to use the -Split method is to have a String object returned by Get-Content (alias cat can also be used, actually), and from the resulting String[] object you can use the brackets to extract the nth item.
Note: Using -Split without parenthesis around Get-Content won't work since -Split is not a parameter name for that command... 🤷♂️
I suppose you don't want to just split the lines, but actually create key/value pairs. That could be achieved like this:
$config = #{}
Get-Content 'C:\path\to\sample.cfg' | % {
$key, $value = $_ -split ':', 2
$config[$key] = $value
}
You could also use the ConvertFrom-StringData cmdlet:
Get-Content 'C:\path\to\sample.cfg' | % {
ConvertFrom-StringData ($_ -replace ':','=')
}
The -replace operation is necessary, because ConvertFrom-StringData expects key and value to be separated by =. If you could change the delimiter in the config file from : to =, you could use ConvertFrom-StringData $_ without replacement.
Very simple question here, I want to see how can we process a bunch of commands using foreach on the command line (not through a PS1 script).
For instance, I display the directory listing on the console, now I want to execute 2 commands per object.
Get-ChildItem | ForEach-Object { Write-Host $_ $_}
This is ok, it shows the filename twice, but lets say I wanted to run 2 Write-Host commands for the same object, would that be possible on the console?
PS: I'm trying to achieve writing an output to 2 files using the out-file cmdlet, so I can read something and have 2 separate out-file calls per object
Thanks
you can script in the console windows just as you would in a powershell file. Use the "`" (backtick) key to separate lines. e.g.:
PS > Write-Host `
>>> hello, world!
So you could do
PS > Get-ChildItem | ForEach-Object { `
>>> Write-Host $_ `
>>> doFoo() `
>>> doBar() `
>>> ` }
Basically you want to execute 2 commands in ForEach-Object statement, right?
Just use ; to separate commands in this way ForEach-Object { command1; command2 }
In your code it should be something like this
Get-ChildItem | ForEach-Object { Write-Host $_; Write-Host $_ }
I am trying to change dates programmatically in a file. The line I need to fix looks like this:
set ##dateto = '03/15/12'
I need to write a powershell V2 script that replaces what's inside the single quotes, and I have no idea how to do this.
The closest I've come looks like this:
gc $file | ? {$_ -match "set ##dateto ="} | % {$temp=$_.split("'");$temp[17]
=$CorrectedDate;$temp -join ","} | -outfile newfile.txt
Problems with this: It gives an error about the index 17 being out of range. Also, the outfile only contains one line (The unmodified line). I'd appreciate any help with this. Thanks!
You can do something like this ( though you may want to handle the corner cases) :
$CorrectedDate = '10/09/09'
gc $file | %{
if($_ -match "^set ##dateto = '(\d\d/\d\d/\d\d)'") {
$_ -replace $matches[1], $CorrectedDate;
}
else {
$_
}
} | out-file test2.txt
mv test2.txt $file -force
I'm trying to do some processing on the output from a psftp "ls" command. Basically, I want to put all of the file names that match certain conditions into an array. I have this:
PS C:\path\to\pwd> $a = & "C:\Program Files (x86)\PuTTY\psftp.exe" -l myusername -batch -pw mypassword -b sftpbatch.txt myserver | where {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[0] -eq "-rw-r--r--"} | select-object {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[8]}
(If you want more details about that command, I can provide them. The output is very similar to the output of the "ls" command in PowerShell.)
It seems to me that I can do better by selecting the split first, then filtering it with where. When I try this:
$a = & <# ... #> | select-object {$_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')} | where { $_[0] -eq "-rw-r--r--" }
I get
Unable to index into an object of type System.Management.Automation.PSObject.
How can I simplify this?
Something like this should work.
$a = & <#...#>| % {$b = $_ -split ' '|?{$_}; if($b[0] -eq '-rw-r--r--'){$b[8]}}
If you are placing this in a script, I would replace the alias % with Foreach-Object, and the alias ? with Where-Object
Edit:
Here is a more pipeline-oriented approach:
'-rw-r--r-- 1 2 3 4 5 6 7 test.txt'|
select #{Name='Words'; Expression={$_ -split ' '|where {$_}}}|
where {$_.Words[0] -eq "-rw-r--r--"}|
foreach {$_.Words[8]}