Powershell automatic variables - variables

So I got this code :
$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11 = $list[0].split(" ")
but is too much, and I think I can automate the creation of $t variables.
How is the correct procedure? with loop but to have the same result.
for ($i=0;$i -le 21; $i++) {
$objResult += New-Object -TypeName PSObject -Property #{"C$i" = ($list[0].split(" "))[$i]}
}
or with
set-variable -name "T$i" -value ($list[0].split(" "))[$i]

If these are simple variables, Set-Variable works perfectly. like so:
for ($i = 0; $i -lt 3; $i++) {
set-variable -name "test$i" -value $i
}
$test0
$test1
$test2
PS > .\test.ps1
0
1
2
If you are looking to work with somewhat more complicated variables (such as COM objects for instance) then New-Object is what you need.

Related

How can I restart the list inside of a word table using PowerShell?

So, I have been trying to iterate through a table in word, starting with the second row and 4th column. I created a macro in the document that restarts a list, "ListRestart_2023."
I tried the following and nothing happened
# Initialize Word application
$word = New-Object -ComObject Word.Application
$word.Visible = $True
Unblock-File "C:\temp\temp.docx"
$docDoc = $word.Documents.Open("C:\temp\temp.docx")
Write-Host "doc full name: " $docDoc.FullName
$docTable = $docDoc.Tables[3]
for ($i = 2; $i -le $docTable.Rows.Count; $i++) {
for ($j = 4; $j -le $docTable.Columns.Count; $j++) {
$word.Application.Run("ListRestart_2023")
}
}
#save and close the destination document
$docDoc.Save()
$docDoc.Close()
# Quit the Word application
$word.Quit()
Is there a better way to do this? I tried this too:
for ($i = 1; $i -le $docTable.Rows.Count; $i++) {
for ($j = 1; $j -le $docTable.Columns.Count; $j++) {
$listParagraphs = $docTable.Cell($i,$j).Range.ListParagraphs
foreach($listParagraph in $listParagraphs)
{
$listParagraph.Range.ListFormat.RestartNumbering()
}
}
}
And this,
$docTable.Cell($i,$j).Range.ListFormat.RestartNumbering()
But, I haven't been able to get anything to happen.

PowerShell Change Variable in ScriptBlock

I'm trying to change a variable inside a ScriptBlock.
What am I doing wrong?
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)
$Window.Add_SourceInitialized( {
$timer = new-object System.Windows.Threading.DispatcherTimer
$timer.Interval = [TimeSpan]"0:0:0.25"
$timer.Add_Tick( $updateBlock )
$timer.Start()
} )
$count = 0
$updateBlock = { Write-Host $count; $count++; Write-Host $count}
The Output is a repeating sequence of 0 and 1. So how do I access the variable and not only a copy of it?
When you modify $count inside the scope of the ScriptBlock, a local copy is created, and the original $Count variable in the parent scope is left untouched.
There are a few ways to modify $count in the parent scope, either with an explicit scope qualifier:
$updateBlock = { Write-Host $count; $script:count++; Write-Host $count}
Or by retrieving the variable with Get-Variable and the relative -Scope parameter (-Scope 1 refers to the immediate parent scope):
$updateBlock = { Write-Host $count; (Get-Variable -Scope 1 -Name count).Value++; Write-Host $count}
Or (as pointed out by #PetSerAl), use the [ref] keyword:
$updateBlock = { Write-Host $count; ([ref]$count).Value++; Write-Host $count}

Have many functions add to same array variable

I have some pseudo code similar to the below
$funcValues = #(New-Object -TypeName psobject -Property #{'Function' = ''; 'Value' = '';})
function func1(){
while($i -lt 5){
$funcValues += #(New-Object -TypeName psobject -Property #{'Function' = 'func1'; 'Value' = $i;})
$i++
}
}
function func2(){
while($i -lt 3){
$funcValues += #(New-Object -TypeName psobject -Property #{'Function' = 'func2'; 'Value' = $i;})
$i++
}
}
func1
func2
$funcValues | Export-CSV C:\path\results.csv -noType
The goal is to have both functions add to the array and after calling the functions export the array to a csv. However when this code is inside a function, it doesn't write anything to the array, but if the code is outside a function, it works.
I'm guessing this has to do with variable scoping, but I'm very unfamiliar with how scoping works in powershell.
Your guess is correct. Try:
function func1(){
while($i -lt 5){
$script:funcValues += #(New-Object -TypeName psobject -Property #{'Function' = 'func1'; 'Value' = $i;})
$i++
}
}
Note that you are creating an array of arrays. If that's not what you wanted, then use:
$script:funcValues += New-Object -TypeName psobject -Property #{'Function' = 'func1'; 'Value' = $i;}
And if you are using V3, you can simplify some more:
$script:funcValues += [pscustomobject]#{'Function' = 'func1'; 'Value' = $i;}
One last comment on the code - using += on an array isn't very fast. Arrays can't be resized, so += will create a new array, copying the elements from the original array and adding the elements at the end. If the array is small, then the syntax is clear and convenient, but if the arrays get large and performance matters, you might consider using a different data structure like ArrayList.

Dynamically create variables in PowerShell

How do I create a new variable each time a loop runs?
Something along the lines of
for ($i=1; $i -le 5; $i++)
{
$"var + $i" = $i
write-host $"var + $i
}
Use New-Variable and Get-Variable (mind available options including scopes). E.g.
for ($i=1; $i -le 5; $i++)
{
New-Variable -Name "var$i" -Value $i
Get-Variable -Name "var$i" -ValueOnly
}

How to pass the parameter in SQL query from PowerShell

I have this code in PowerShell, that executes SQL query to UPDATE my table:
$Connection=new-object data.sqlclient.sqlconnection "server=server;database=mydb;trusted_connection=true;"
$Connection.open()
For ( $i = 0; $i -le $ActID.Length; $i ++ ) {
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.Connection = $Connection
$cmd.CommandText =
"
update Table
set Note = #PATH
"
$cmd.Parameters.Add("#PATH", $ActID[$i].Values) | Out-Null
$cmd.ExecuteNonQuery()
}
I tried to update the table with the variable defined in this string:
$cmd.Parameters.Add("#PATH", $ActID[$i].Values) | Out-Null
But when I execute the script the error log says that there is no value passed in $ActID[$i]
Are there other methods to pass parameters (variables) in powershell queries?
What could be the mistake:
$i -le $ActID.Length;
it should be probably
$i -lt $ActID.Length;
You could also use piping which simplifies the code:
$actId | % { ..... $cmd.Parameters.Add("#PATH", $_.Values) | Out-Null .... }
Besides that the property you use is Values - is it really what you wanted? Values looks like a collection of something. Maybe you wanted to use a single value.