Vilestorm
Vilestorm

Reputation: 55

Powershell: Storing variables in a directory to be called back inside a For command

I am relatively new to powershell coding, i am trying to create a script which auto runs a system key grab. However i am struggling with the naming system. I am using a directory of previous tests but cant seem to figure out a way to call it with my script. Im trying to have my $Num variable change and stay changed after my script is run.

gci -Recurse -Path E:\temp | Measure-Object.Count
for($Num.Count ; $Num -le 300; $Num++){

Write-Output Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
start-sleep -Seconds 05
Write-Output A
Write-Output Install-Script -Name Get-WindowsAutoPilotInfo -RequiredVersion 3.5
start-sleep -Seconds 5
Write-Output Y
start-sleep -Seconds 5
Write-Output Y
start-sleep -Seconds 5
Write-Output Y
Write-Output Get-WindowsAutoPilotInfo.ps1 | Out-File -FilePath E:\temp\kpf-Laptop$num.txt -NoClobber

Write-Output $Num

Exit


}

Upvotes: 0

Views: 105

Answers (1)

Vilestorm
Vilestorm

Reputation: 55

Answer to storing a variable after a powerscript has run. This script will save and change the $file_data even after the script is closed.

Get-Content D:\pscript\temp\Directory.txt
[int] $file_data = Get-Content D:\pscript\temp\Directory.txt

$wshell = New-Object -ComObject wscript.shell;

for ([int]$file_data -le 130; $file_data++ ){
Write-Output Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
start-sleep -Seconds 1
$wshell.SendKeys('~')
start-sleep -Seconds 5
Write-Output A
Write-Output Install-Script -Name Get-WindowsAutoPilotInfo -RequiredVersion 3.5
start-sleep -Seconds 1
$wshell.SendKeys('~')
start-sleep -Seconds 5
Write-Output Y
start-sleep -Seconds 5
Write-Output Y
start-sleep -Seconds 5
Write-Output A
Write-Output Get-WindowsAutoPilotInfo.ps1 | Out-File -FilePath D:\pscript\AutoPilot\Laptop$file_data.csv -NoClobber


Set-Content D:\pscript\temp\Directory.txt $file_data


exit
}

Upvotes: 1

Related Questions