Reputation: 77
I'm currently trying to make ps1 script that creates another ps1 script via echo, add-content, clip and so forth.
What I'm trying to do is write a ps1 script that gets a folder, stores it in a variable ($VBS
) and writes the new ps1 to a file in that folder to afterwards run it from that folder. Here's my code so far:
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }
cd \
$VBS = gci -Recurse -Filter "Folder" -Directory -ErrorAction SilentlyContinue -path "C:\"
cd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
echo $VBS | ForEach-Object { $_.FullName } > KickStart.ps1
cat KickStart.ps1 | set-clipboard
echo '"if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }' > KickStart.ps1
add-content KickStart.ps1 "`ncd \"
add-content KickStart.ps1 "`ncd"
add-content KickStart.ps1 ' "'
add-content KickStart.ps1 | get-clipboard
add-content KickStart.ps1 '"'
add-content KickStart.ps1 '`nstart File.vbs'
add-content KickStart.ps1 '`n cd \'
add-content KickStart.ps1 '`ncd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"'
add-content KickStart.ps1 'rm KickStart.ps1'
The final product (KickStart.ps1) should look like this:
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }
cd \
cd "C:\PATH\TO\FOLDER"
start file.vbs
cd \
cd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
rm KickStart.ps1
Instead my current scripts generates this:
"if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }
cd \
cd
"
"
`nstart File.vbs
`n cd \
`ncd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
rm KickStart.ps1
I seriously don't know what I'm doing wrong, I've put in almost 12 hours finding the solutions, help would be appreciated.
Upvotes: 0
Views: 190
Reputation: 61253
Although not entirely sure what you are trying to achieve, but looking at the desired output, why not build the content using a Here-String like this:
@'
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
Start-Process powershell.exe -WindowStyle Hidden -NoProfile -ExecutionPolicy RemoteSigned -File "$PSCommandPath" -Verb RunAs
exit
}
Set-Location \
Set-Location "C:\PATH\TO\FOLDER"
Start-Process file.vbs
Set-Location \
Set-Location "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start-Process Menu\Programs\Startup"
Remove-Item KickStart.ps1
'@ | Set-Content -Path 'THE PATH FOR THE KickStart.ps1 FILE'
Result:
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
Start-Process powershell.exe -WindowStyle Hidden -NoProfile -ExecutionPolicy RemoteSigned -File "$PSCommandPath" -Verb RunAs
exit
}
Set-Location \
Set-Location "C:\PATH\TO\FOLDER"
Start-Process file.vbs
Set-Location \
Set-Location "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start-Process Menu\Programs\Startup"
Remove-Item KickStart.ps1
Upvotes: 1