The PowerShell code works when run from the PowerShell command prompt but not when run with double-click or turned into an executable - windows-10

This code:
$username = 'Username'
$password = 'Password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process -FilePath powershell -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList "-NoProfile -Command `"Start-Process -FilePath wscript -Verb RunAs -ArgumentList '\`"$((Get-Location).Path -replace "'", "''")\test.vbs\`" \`"/CurrentDirectory:$((Get-Location).Path -replace "'", "''")\`" \`"/AppData:$($env:APPDATA -replace "'", "''")\`"'`""
works when I run it line by line from the PowerShell command prompt.
The problem occurs both when I associate the .ps1 extension to "Windows PowerShell" and then double-click on the script, and when I compile the script in .exe through IronMan Software's "PSScriptPad".
A similar problem is present at the following link:
Powershell script executes correctly when I choose "run with powershell", but not when I "open with" powershell or double-click it
but in my case it doesn't even work with "Run with PowerShell" and perhaps the cause of the problem is the same one that also affects executables.
Another useful link is the following:
https://stackoverflow.com/a/58245206/45375
How can I solve the problem?
Windows 10 Pro 64-bit
Powershell Version: 5.1.19041.1237 (Integrated in Windows 10).

The compiled executable works correctly when it is not in a directory containing the apostrophe characters while in the case of double-clicking on a .ps1 script, the directory containing it cannot even contain spaces.
This is a useful link:
https://social.technet.microsoft.com/Forums/en-US/bf3344de-3af6-48e3-9f43-f595bb41c62d/bug-powershell-starts-w-error-when-opened-by-context-menu-in-folder-w-apostrophe-in-its-name?forum=win10itprogeneral
For the apostrophes path problem in .exe files, I sent a bug report to IronMan Software PSScriptPad, which was then taken care of.
For the problem of paths with apostrophes or consecutive whitespaces when double-clicking or doing "Run with PowerShell" on .ps1 scripts, I have solved by fixing the following registry values:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.ps1]
#="Microsoft.PowerShellScript.1"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""
[HKEY_CLASSES_ROOT\Directory\Shell\Powershell\command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""
[HKEY_CLASSES_ROOT\Drive\shell\Powershell\command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\",0"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force }; & \\\"%1\\\"\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\0\Command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force }; & \\\"%1\\\"\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit\Command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe\" -File \"%1\""
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"ExecutionPolicy"="RemoteSigned"

Related

Npm powershell script stuck on 1 task in Azure DevOps

So I'm using the inline script on Azure DevOps, what variables or string should I add to continue to the next script once the npm run start completed, because at the moment my pipeline hangs at after starting the script and not gong to the next frontend script :
$backendFolder = "C:\path\to\backend"
$nodejsExecutable = "C:\Program Files\nodejs\npm.cmd"
Set-location $backendFolder
Start-Process -FilePath $nodejsExecutable -ArgumentList "run","build" -wait -NoNewWindow -ErrorAction Stop -Verbose -WorkingDirectory $backendFolder
Start-Process -FilePath $nodejsExecutable -ArgumentList "run","start" -wait -NoNewWindow -ErrorAction Stop -Verbose -WorkingDirectory $backendFolder
What am I missing here?
Notes # I've tried a lot of different combinations of the Start-Process command but still no go, I've also followed a couple of blog post, not going to share each and everyone here, as it's a lot but here is 2 > https://lazyadmin.nl/powershell/start-process/ & https://ss64.com/ps/

azure VM extension Powershell Custom script

I want to pass the variables inside my terraform script when I call the PowerShell script inside "azurerm_virtual_machine_extension". i don't know how to pass arguments to my powershell script. I have used the following code.
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -file ${var.main_hybridscriptname} -AAResourceGroupName ${var.aobclResourceGroup} -OMSResourceGroupName ${var.aobclResourceGroup} -SubscriptionID ${var.azure_subscription_id} -AutomationAccountName ${module.automationAccount.AutomationName} -HybridGroupName ${var.hybridgroupname} -WorkspaceName ${azurerm_log_analytics_workspace.aobclloganalyticsworkspace.name} -appidSPN ${var.SPN_APP_ID} -SPNpswd ${var.SPN_PSWD} -tenantID ${var.azure_tenant_id}"
error in script execution when i use a script of test without argument, it worksstrong text

Running an EXE from azure blob storage with a configuration xml

could someone please help me with this? I have issues running an EXE from azure blob storage combined with a configuration file. The exe is called from the blob and then needs an argument that calls an xml configuration file in the same blob directory as the exe file. The exe is called fine but the config file is not. Any ideas?
If i run the c:\temp\setup.exe /configure configuration.xml it runs fine so i'm really not sure?
$Path = $env:TEMP
$Installer = "setup.exe"
$OfficeConfig = "configuration.xml"
Invoke-WebRequest "https://********.blob.core.windows.net/office1/setup.exe?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-07-16T16:00:03Z&st=2019-07-16T08:00:03Z&spr=https&sig=7sO7m3PJMne8hFYAwHY6YFZZG6AfUuhC8x%2BoR7QkWuY%3D" -OutFile $Path\$Installer
Invoke-WebRequest "https://********.blob.core.windows.net/office1/Configuration.xml?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-07-16T16:00:03Z&st=2019-07-16T08:00:03Z&spr=https&sig=7sO7m3PJMne8hFYAwHY6YFZZG6AfUuhC8x%2BoR7QkWuY%3D" -OutFile $Path\$OfficeConfig
Start-Process -FilePath $Path\$Installer -Argumentlist'/configure $Path\$OfficeConfig' -Verb RunAs -Wait
Remove-Item $Path\$Installer
You should change the single quote to double quote for the -Argumentlist parameter in the Start-Process cmdlet, like below:
change -Argumentlist '/configure $Path\$OfficeConfig' to -Argumentlist "/configure $Path\$OfficeConfig"

Start Process with arguments

I am trying to create a script that would allow users to start an online repair of Office, I am checking if they have 32 or 64 bits then the language.
I made the following script but nothing happens and I don't know why
$culture = (Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration)."ClientCulture"
$platform = (Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration)."Platform"
$CMD = "C:\Program Files\Common Files\microsoft shared\ClickToRun\OfficeClickToRun.exe"
Start-Process -FilePath $CMD -ArgumentList #("scenario=Repair", "platform=$platform", "culture=$culture", "forceappshutdown=True", "RepairType=FullRepair", "DisplayLevel=True") -Wait

IIS: how to undeploy/delete/remove a webapp from command line?

Suppose there's a webapp deployed on local IIS server. When I need to remove/undeploy it, I can go to IIS Manager, right-click on the app, and then select "Delete application and content" - et voila. But, I need to do the same from the command line - how? It can be assumed that the name of the application is known.
Maybe this can be done via MSDeploy somehow?
If you just want to remove the application from the Web Site in IIS without physically deleting the files (like msdeploy does) or if you don't have the WebDeploy-extension installed, you can use the following command:
C:\Windows\System32\inetsrv\appcmd.exe delete app "Default Web Site/MyAppName"
This is what did it:
"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy" -verb:delete -dest:apphostconfig="Default Web Site/<webapp_name>"
I know the question says "command line", but you can use PowerShell and the IIS Administration Cmdlets to do this task. I provide all of the functions and explain the process of how to automate this on my blog. Also, you can easily swap out the IIS Administration Cmdlet calls with calls to msdeploy, appcmd, IIsVdir.vbs, etc.
For your specific question, this PowerShell code should do the trick:
$block = {
Import-Module WebAdministration
$website = "YourWebsiteName"
$applicationName = "PathUnderWebsite\ToYourApplication"
$fullPath = Join-Path $website $applicationName
Write-Host "Checking if we need to remove '$fullPath'..."
if (Get-WebApplication -Site "$website" -Name "$applicationName")
{
Write-Host "Removing '$fullPath'..."
Remove-WebApplication -Site "$website" -Name "$applicationName"
}
Write-Host "Deleting the directory '$fullPath'..."
Remove-Item -Path "IIS:\Sites\$fullPath" -Recurse -Force
}
$session = New-PSSession -ComputerName "Your.WebServer.HostName"
Invoke-Command -Session $session -ScriptBlock $block
Remove-PSSession -Session $session
iisweb /delete WebSite [/s Computer [/u [Domain ]User /p Password ]]

Resources