# Error from command-line, not interactively

**URL:** <https://forums.powershell.org/t/error-from-command-line-not-interactively/12636>\
**Category:** PowerShell Help\
**Created:** [May 29, 2019, 8:24am UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636 "2019-05-29T08:24:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebredmond](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ebredmond](https://forums.powershell.org/u/ebredmond)\
**Post date:** [May 29, 2019, 8:24am UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636/1 "2019-05-29T08:24:41Z")

</div>

I am currently trying to run the following script :

```
add-type "using System.Net;using System.Security.Cryptography.X509Certificates;public class TrustAllCertsPolicy : ICertificatePolicy {public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest request, int certificateProblem) {return true;}}";$AllProtocols = [System.Net.SecurityProtocolType]"Ssl3,Tls,Tls11,Tls12";[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols;[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy;$server = "x.x.x.x";$urlbase = "https://${server}:port/api/";$username = "Prop";$password = "Testpassword";$EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $password);$EncodedPassword = [System.Convert]::ToBase64String($EncodedAuthorization);$headers = @{"Authorization"="Basic $($EncodedPassword)"};$url = $urlbase + "site/custom/PropFiles/files";[xml]$r2 = Invoke-RestMethod -Uri $url -Method GET -Headers $headers;$link = ($r2.BESAPI.SiteFile | where {$_.Name -ieq "manifest"}).Resource;Invoke-RestMethod -Method Put -Uri "$link`?force=true;isClientFile=true" -infile "C:\users\Administrator\Manifest" -ContentType "text/txt" -Headers $headers
```

Using the following syntax:

powershell.exe -executionpolicy bypass -command {_command listed above_}

And I get the following error:

```
ERROR: Invalid pattern is specified in "path:pattern".
```

However, if I run this same script using powershell interactively (not using the ISE), it runs with no issues. Any thoughts as to why it produces the error when running from command line, but not when running it interactively?

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 29, 2019, 12:18pm UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636/2 "2019-05-29T12:18:05Z")

</div>

put it in a file and use -f instead of -command, then pass the file path to it.

---

<div class="post-metadata">

**Author:** ![ebredmond](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ebredmond](https://forums.powershell.org/u/ebredmond)\
**Post date:** [May 29, 2019, 7:21pm UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636/3 "2019-05-29T19:21:31Z")

</div>

With creds being in the script I am not wanting to save the script to disk.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 29, 2019, 9:28pm UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636/4 "2019-05-29T21:28:27Z")

</div>

Can’t you prompt for the password ? how are you executing the script, is it in a scheduled task ?

---

<div class="post-metadata">

**Author:** ![rejikodiyil](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@rejikodiyil](https://forums.powershell.org/u/rejikodiyil)\
**Post date:** [May 29, 2019, 10:37pm UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636/5 "2019-05-29T22:37:08Z")

</div>

Well, this looks like a complex code (multiple double quotes and other notation) in a script block, this can be done but takes time.

&nbsp;

I would suggest to save this in a script file and execute. To solve the password problem, you can encrypt your password with below code

```
ConvertTo-SecureString -AsPlainText "Testpassword" -Force | ConvertFrom-SecureString | Out-File -FilePath C:\Temp\restHash.txt
```

Later you can decrypt it inside the script.

```
$username = "Prop"
$password = Get-Content C:\Temp\restHash.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential ($username , $password)
$EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $cred.GetNetworkCredential().Password)
```

One point to remember, encryption and decryption will work only with the same user context. To decrypt the password in a different user context, you may need to encrypt with a key or certificate.

Hope this helps

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/error-from-command-line-not-interactively/12636/6 "2024-05-16T20:32:43Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


