I am trying to create a PowerShell command that reads a registy variable and then executes a command. It does this from the command line using CMD and it runs under SYSTEM.
I have the following code:
powershell.exe -ExecutionPolicy ByPass -WindowStyle Minimized -Command
"$ErrorActionPreference = 'silentlycontinue' |`
New-Variable -name UserNameForSapFolderRemoval -Value (get-itemproperty 'HKCU:\Volatile Environment' |`
Select-Object -ExpandProperty UserName) |`
Remove-Item $("C:\Users\"+$UserNameForSapFolderRemoval +"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Sapgui 7.30") -Force -Recurse |`
Remove-Variable -Name UserNameForSapFolderRemoval"
But it returns with:
The string is missing the terminator
I added the " character but without succes.
Any idea how I can get this powershell command to run succesful?
Parsing username from registry is way too complicated, as there already is $env:username. Try something like this,
powershell -command { rm -force -recurse "c:\users\$env:username\appdata\whatever" }
Related
Why do I get the error "MissingVariableNameAfterForeach" for the following:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "foreach($file in Get-ChildItem C:){((Get-Date)-$file.LastWriteTime).ToString('dd')}"
The command is supposed to print out the day since today of the last file/folder write in C:\
From the help text:
PS C:\> powershell.exe /?
If the value of Command is a script block, the script block must be enclosed
in braces ({}). You can specify a script block only when running PowerShell.exe
in Windows PowerShell.
Try this:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command {Get-ChildItem C: | ForEach-Object{($_.LastWriteTime).ToString('dd')}}
Your command will work if you enclose the Get-ChildItem in parentheses.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
"foreach($file in (Get-ChildItem C:)){((Get-Date)-$file.LastWriteTime).ToString('dd')}"
I may have understood your question, because the existing answers do not seem to provide the information I thought you were looking for.
Whilst these examples don't specifically answer the question posed in your title, they are intended to output what I think you were looking for.
Here therefore is my batch-file attempt:
#"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command^
"Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime|"^
"Select-Object -Last 1|Format-Table -AutoSize -Property Name,"^
"#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
#Pause
And obviously the cmd version, to be on topic:
"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime|Select-Object -Last 1|Format-Table -AutoSize -Property Name,#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
Just in case it is simply my misunderstanding, perhaps this batch-file may work for you:
#"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command^
"Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime -Descending|"^
"Format-Table -AutoSize -Property Name,"^
"#{Name='DayInMonth';Expression={($_.LastWriteTime).ToString('dd')}},"^
"#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
#Pause
cmd version:
"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-ChildItem -Path 'C:\'|Sort-Object -Property LastWriteTime -Descending|Format-Table -AutoSize -Property Name,#{Name='DayInMonth';Expression={($_.LastWriteTime).ToString('dd')}},#{Name='DaysOld';Expression={[Int]$((Get-Date)-$_.LastWriteTime).TotalDays}}"
In both cases, you'll note because I'm not running a PowerShell script, there is no need to stipulate an execution policy. Commands should work as if being run directly in the PowerShell window.
I'm fairly new to powershell.
In this code my coworkers are getting an error which I'm not getting.
I've tried some different things (like adding the complete path to powershell) but I'm not able to fix it.
Can anyone tell me what's going wrong?
Function Button4_Click()
{
$Filepathmmc = join-path -path $env:SystemRoot -ChildPath System32\Mmc.exe
$argumentlistdsa = join-path -path $env:SystemRoot -ChildPath System32\dsa.msc
$Credential = New-Object -TypeName System.Management.Automation.PsCredential -ArgumentList (Get-Content "$($env:userprofile)\username.txt"),(Get-Content "$($env:userprofile)\encrypt.txt" | ConvertTo-SecureString)
Start-Process powershell -Credential $Credential -ArgumentList "Start-Process -FilePath '$filepathmmc' -ArgumentList '$argumentlistdsa' -Verb RunAs"
}
I also tried using this as the Start-Process path:
$aducpath = (Join-Path $Env:SystemRoot -ChildPath "\syswow64\WindowsPowerShell\v1.0\powershell.exe")
Needs elevation
I see that you are still using join-path, have you tried using an absolute path for the sake of troubleshooting?
I also see that you're reading a password from a text file. I would advise you not to do that, and instead maybe have users log in themselves instead for elevation. If you replace $Credential in the invocation process with (Get-Credential) it will prompt your user for elevation. You can also use $Credential = Get-Credential to save it for the future.
I can't reproduce the error you're getting with just this code snippet. All of your Join-Paths are sane and seem to point to the expected place. When run as an unprivileged user, I get a UAC prompt rather than the invalid code. If there is actually a problem with where things are, might I suggest that you use something like this to search for what you need?
Function Button4_Click()
{
$Filepathmmc = (Get-ChildItem -Recurse -Path $env:SystemRoot -Filter "Mmc.exe").FullName | Select-Object -First 1
$argumentlistdsa = (Get-ChildItem -Recurse -Path $env:SystemRoot -Filter "dsa.mmc").FullName | Select-Object -First 1
Start-Process powershell -Credential (Get-Credential) -ArgumentList "Start-Process -FilePath '$filepathmmc' -ArgumentList '$argumentlistdsa' -Verb RunAs"
}
It's not perfect, but it will always return an instance of mmc. You can add -ErrorAction SilentlyContinue to the Get-ChildItem command to suppress the inevitable 'Accesss to the path X is denied' exceptions.
I am trying to use wildcards like "c:\scripts\*.ps1" in VisibleExternalCommands for New-PSSessionConfigurationFile and the resulting .pssc file looks fine, but cannot get it to work as expected and the documentation is weak here. I can only make VisibleExternalCommands work by giving full path.
Steps to reproduce, tested on Win10 with PowerShell 5.1:
Setup directory and scripts:
Enable-PSRemoting -Force
mkdir c:\scripts
echo "Write-Output hello" | Out-File c:\scripts\hello.ps1 -Force
echo "Write-Output world" | Out-File c:\scripts\world.ps1 -Force
Using Wildcard (NOT working):
New-PSSessionConfigurationFile -VisibleExternalCommands "c:\scripts\*.ps1" -Path c:\scripts\TestWildcard.pssc -SessionType RestrictedRemoteServer -LanguageMode FullLanguage
Register-PSSessionConfiguration -Name TestWildcard -Path c:\scripts\TestWildcard.pssc -Force
Invoke-Command . -ConfigurationName TestWildcard {c:\scripts\hello.ps1; c:\scripts\world.ps1}
# Fails to recognize both .ps1 scripts as external commands
Using Full Path (works fine):
New-PSSessionConfigurationFile -VisibleExternalCommands "c:\scripts\hello.ps1" -Path c:\scripts\TestFullPath.pssc -SessionType RestrictedRemoteServer -LanguageMode FullLanguage
Register-PSSessionConfiguration -Name TestFullPath -Path c:\scripts\TestFullPath.pssc -Force
Invoke-Command . -ConfigurationName TestFullPath {c:\scripts\hello.ps1; c:\scripts\world.ps1}
# Prints hello and fails to recognize c:\scripts\world.ps1
The documentation (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssessionconfigurationfile?view=powershell-5.1) says that wildcard characters are supported, but shows no example of it.
Am I missing something or is this a bug?
The VisibleExternalCommands option does not accept wildcards. This documentation bug was fixed in https://github.com/MicrosoftDocs/PowerShell-Docs/issues/4756.
I have the following NSIS (.nsi) script that wraps PowerShell script into exe.
In addition, I would like the exe to run as admin because the scripts needs to update registry key.
NSIS Script is:
!include x64.nsh
RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
OutFile "file.exe"
SilentInstall silent
Section
SetOutPath $EXEDIR
File "file.ps1"
# Run the script to update
ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File .\file.ps1"
SectionEnd
Function .onInstSuccess
Delete "file.ps1"
FunctionEnd
PowerShell script is:
$registryPath = "HKLM:\SOFTWARE\Test"
$Name = "keyName"
$value = "keyValue"
$preRegVer = (Get-ItemProperty $registryPath).Version
#log "Pre registry value: $preRegVer"
If(!(Test-Path $registryPath))
{
# log "Path does not exist"
New-Item -Path $registryPath -Force | Out-Null
# log "Path created"
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
# log "Value created"
}
Else {
# log "Path exist"
$val = Get-ItemProperty -Path $registryPath
if($val.Version -eq $null)
{
# log "Value does not exist"
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
# log "Value created"
}
Else {
# log "Value exist"
Remove-ItemProperty -path $registryPath -Name Version -Force
# log "Value removed"
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
# log "Value created"
}
}
When I run .exe file, it asks for elevated permission, but does not update the key.
I know that powershell script works, because I used PowerGUI to compile it to exe, and it updates the key.
Only problem with PowerGUI is that it does not have an option to run as Admin.
I am suspecting you're running on 64bit machine and there is conflict with bitness.
Haven't tried this, but try this ans see if that works.
${If} ${RunningX64}
${DisableX64FSRedirection}
${EndIf}
ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File .\file.ps1"
${If} ${RunningX64}
${EnableX64FSRedirection}
${EndIf}
Instead of using power shell script, you can perform same task in NSIS itself. You can modify and you can create your own new registry entry using nsis.
for example you can use following commands to write and read the registry
WriteRegStr HKLM SOFTWARE\NSIS_Example2 "Version" "1.0"
ReadRegStr $mm HKLM "SOFTWARE\NSIS_Example2" "Version"
Here's a link!
The end result of our build process calls NSIS to create an executable for the product(s) we're building. We too are trying to call Powershell similar to the example listed above to run at deploy time, the powershell script is called to manage special duties on the server that are related to IIS.
In the NSIS file we've tried the variations below with no success:
nsExec::ExecToStack 'powershell -inputformat none -ExecutionPolicy bypass -File "C:\HardCodedLocation_Instance.ps1" '
nsExec::ExecToStack 'powershell.exe "& "C:\HardCodedLocation_Instance.ps1"' $0
ExecWait 'powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File C:\HardCodedLocation_Instance.ps1' $0
${PowerShellExec} "C:\HardCodedLocation_Instance.ps1"
The "C:\HardCodedLocation_Instance.ps1" is getting run by NSIS at deploy time but the tasks that require administrative permissions in the script are not being completed.
The first two lines in "C:\HardCodedLocation_Instance.ps1":
Set-ExecutionPolicy -ExecutionPolicy Bypass
Import-Module webadministration
I'm running the executable logged onto a server as an administrator. I can then turn around, right click on "C:\HardCodedLocation_Instance.ps1" and "Run with PowerShell" and it works as it should.
I am trying to create a context menu but once its created i get an error message stating
This file does not have a program associated to it.
i am using this script . I am trying to create a powershell shortcut on folders.
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Test-Path HKCR:\Directory\shell\Powershell
New-Item -Path HKCR:\Directory\shell -Name Powershell
Set-Item -Path HKCR:\Directory\shell\Powershell -Value "Open Powershell Here"
New-Item -Path HKCR:\Directory\shell\Powershell\key -Value
"C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -
LiteralPath '%L'"
Try to change last line:
New-Item -Path HKCR:\Directory\shell\Powershell\key -Value "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command ""Set-Location -LiteralPath '%L'"""
If not work add a third '"' """Set-location...