Powershell loop values of multiple variable in array - powershell-4.0

Trying to output multiple variables here in one and then email.
I keep getting one for obvious reason that its not storing all values.
Can you please help?
foreach ($computer in $computer_list)
{
# Write-Host $computer
$file = Get-childItem "\\$computer\c$\Users\test.log" -Include *log
$size = $file.Length/1gb
for ($i=0;$i -lt $computer.ReadCount; $i++)
{
$body += "$computer"
$body += "$file"
$body += "$size"
}

Related

Passing Hashes to a Powershell function problem

I must be missing something. i have to variables:
$var1 and $var2
$var1 | gm
TypeName: System.Collections.Hashtable
Each of them has IP and Port property, for example $var1[0].ip = '1.1.1.1'; $var1[0].ports = #(22,23,24)
Now I want to create a function that does some comparing between those 2 objects:
Function CompareData ($data1,$data2){
$data1 | gm #this is just for me to test whats wrong
write-host "first data $data1.ip" #just for me
write-host "Second data $data2.ip" #just for me
$str =''
#check each ip in data2 if it exists in data1
#if it exists, start checking for ports
#if not exists do bla bla
For ($i=0; $i -lt $data2.count; $i++){
$ip = $data2[$i].ip
for ($j=0; $j -lt $data1.count; $j++){
if ($data1[$j].ip -eq $ip){
$str += "$ip`r`n"
$str += "Base ports: $data1[$i].ports`r`n"
$str += "Current ports: $data2[$j].ports`r`n"
}
}
}
}
the function isnt ready, what i do want to know is why im having a problem passing $var1 and $var2
Im doing this:
CompareData ($var1, $var2)
and it looks like only $var1 is passing while $var2 comes empty, also, if i do (inside function):
$data1 | gm
System.Object[] System.Object[].ip
meaning the function isnt getting the variables as i want. what am i missing here?
When you use parentheses, PowerShell constructs an array with all the values in it, and uses that as the value for one parameter.
So, CompareData ($var1, $var2) is equivalent to CompareData -data1 #($var1, $var2) with no value specified for the -data2 parameter.
If you use CompareData -data1 $var1 -data2 $var2 as suggested by #dwillits you should get the result you expect.
I've had problems with my functions when calling them with parenthesis. Have you tried calling your function like so?
CompareData -data1 $var1 -data2 $var2

How to convert "$#" from bash to Powershell?

I have to change a bash script into a PowerShell script, but I dont really get the condition in the if-statement.
if [ $# -eq 0 ]
then
name='plmapp-all'
else
name="$1"
Does somebody know what's $# and how this statement would look like in PowerShell?
$# is an internal bash variable that holds the number of arguments passed to a script. Its PowerShell equivalent is $args.Count. $args is an automatic variable holding a list with the arguments passed to the script (like $# in bash). Since PowerShell is working with objects you can obtain the argument count directly from the $args array without the need for an additional variable.
The whole expression would look like this in PowerShell:
if ($args.Count -gt 0) {
$name = 'plmapp-all'
} else {
$name = $args[0]
}
You could simplify that to
if ($args) {
$name = 'plmapp-all'
} else {
$name = $args[0]
}
because PowerShell interprets non-empty arrays as $true and empty arrays as $false in a boolean context.
An even more streamlined alternative would be:
$name = #($args + 'plmapp-all')[0]
Since $args is always an array the + operator appends the string to that array, then the index operator selects the first element from the result array.
[] + 'plmapp-all' → [ 'plmapp-all' ]
[ 'something' ] + 'plmapp-all' → [ 'something', 'plmapp-all' ]

How to sort pictures from newest to oldest?

I have this script. It works by taking all the pictures in a folder, and make a webpage of it. Oldest picture on top, and newer pictures down the page.
How can I revert the way the pictures are displayed? I want the newest on top.
<?php
$folder = 'ute/grabs/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
echo '<table>';
for ($i = 0; $i < $count; $i++) {
echo '<tr><td>';
echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
echo '</td></tr>';
}
echo '</table>';
?>
Put this after glob.
usort($files, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));
See: glob() - sort by date
Note: I have changed the first argument of the "create_function" to reverse the order.
Thank you Icarus3
That worked great.
This was the final solution:
<?php
$folder = 'webcam/webcam/ute/grabs/';
$filetype = '*.jpg';
$files = glob($folder.$filetype);
usort($files, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));
$count = count($files);
echo '<table>';
for ($i = 0; $i < $count; $i++) {
echo '<tr><td>';
echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a><br>';
echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
echo '</td></tr>';
}
echo '</table>';
?>

Use map or dictionary to find value

Here is my situation. I have an array of Device id's. I know which device id's go with which ip addresses. I want to use the device id's to find the correct ip address and pass both of them in a command. I am using a shell script to do this.
Here is some psuedo code for what I want.
This is my dictionary type thing matching id's with ip's, no particular order
dictionary_thing = ( id:ip
id:ip
id:ip
id:ip )
This is my array of id that I am using in no particular order
array_of_used_ids = ( id
id
id )
Maybe make an array of objects with two properties (id and ip) for each id found
array_object
for(int i=0; i<count; i++)
{
array_object[i]= new object and set id to dictionary_thing id and ip
}
then I want to run all the commands in a for loop (or whatever you suggest)
for id in array_of_used_ids
./run_this_command with
for(int i=0; i<count; i++)
{
./command array_object[i].ip array_object[i].id
}
You already have your primary arrays built, so build a regex from the used IDs
regex="^${array_of_used_ids[0]}:"
i=1
while [ $i -lt ${#array_of_used_ids[*]} ]; do
regex="$regex|^${array_of_used_ids[$i]}:"
i=$(( $i + 1))
done
This should give you a regex like "^id:|^id:|^id:"
Then iterate your dictionary checking each against the regex, when matched, replace the separating ':' with a space
array_object=()
i=0
j=0
while [ $i -lt ${#dictionary_thing[*]} ]; do
if [[ $dictionary_thing[$i] =~ $regex ]]; then
array_object[$j] = ${dictionary_thing[$i]/:/ } #if it is possible for an ID to contain a ':', you will need a different method for splitting here
j=$(( $j + 1 ))
fi
i=$(( $i + 1 ))
done
Finally, iterate your result object and execute the command
i=0
while [ $i -lt ${#array_object[*]} ]; do
command_to_execute ${array_object[$i]}
i=$(( $i + 1 ))
done

How to re-write powershell code in bash

I've been tasked with re-writing this in bash. But whilst most powershell is easy to read, i just dont get what this block is actually doing!!? Any ideas?
It takes a file which is sorted on a key first, maybe thats relevant!
Thanks for any insights!
foreach ($line in $sfile)
{
$row = $line.split('|');
if (-not $ops[$row[1]]) {
$ops[$row[1]] = 0;
}
if ($row[4] -eq '0') {
$ops[$row[1]]++;
}
if ($row[4] -eq '1') {
$ops[$row[1]]--;
}
#write-host $line $ops[$row[1]];
$prevrow = $row;
}
Perhaps a little refactoring would help:
foreach ($line in $sfile)
{
# $row is an array of fields on this line that were separated by '|'
$row = $line.split('|');
$key = $row[1]
$interestingCol = $row[4]
# Initialize $ops entry for key if it doesn't
# exist (or if key does exist and the value is 0, $null or $false)
if (-not $ops[$key]) {
$ops[$key] = 0;
}
if ($interestingCol -eq '0') {
$ops[$key]++;
}
elseif ($interestingCol -eq '1') {
$ops[$key]--;
}
#write-host $line $ops[$key];
# This appears to be dead code - unless it is used later
$prevrow = $row;
}
You are splitting a line on the '|' charater to an array row. It looks like you are using the $row array as some type of key into the $ops var. The first if test to see if the object exists if it doesn't it creates it in $ops the second and third ifs test to see if the 5th element in $row are zero and one and either increment or decrement the value created in the first if.
Approximately:
#!/bin/bash
saveIFS=$IFS
while read -r line
do
IFS='|'
row=($line)
# I don't know whether this is intended to test for existence or a boolean value
if [[ ! ${ops[${row[1]}] ]]
then
ops[${row[1]}]=0
fi
if (( ${row[4]} == 0 ))
then
(( ops[${row[1]}]++ ))
fi
if (( ${row[4]} == 1 ))
then
(( ops[${row[1]}]-- ))
fi
# commented out
# echo "$line ${ops[${row[1]}]}
prevrow=$row
done < "$sfile"

Resources