Powershell open Ps1 if true - powershell

What am I doing wrong here? I am trying to open a second PS1 file if True but an error is thrown (shown below).
#ABOUT
#GLOBALS
$userID = "admin"
$pswrd = "test"
$name = Read-Host 'What is your username?'
$pass = Read-Host 'And your password?' #-AsSecureString | ConvertFrom-SecureString
#$script = '.\sdsSysMain.ps1'
if($name -eq $userID -and $pass -eq $pswrd) #or blank?
{
#write-host "Well done! You're in. "
#Start-Sleep -s 5
Powershell -noexit ".\sdsSysMain.ps1"
}
elseif($name -ne $userID -or $pass -ne $pswrd)
{
write-host "Login Failed... :("
}
This is the error I'm getting:
powershell.exe : The term '.\sdsSysMain.ps1' is not recognized as the name of a cmdlet, function
At C:\Users\1234\Documents\Projects\sdsSys\sdsSysLogin.ps1:19 char:15
+ Powershell <<<< -noexit ".\sdsSysMain.ps1"
+ CategoryInfo : NotSpecified: (The term '.\sds...mdlet, function:String) [],
RemoteException
+ FullyQualifiedErrorId : NativeCommandError
, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:17
+ .\sdsSysMain.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (.\sdsSysMain.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

The error is rather self-explanatory. The script doesn't find sdsSysMain.ps1 in the current working directory. You can output the current working directory in your script like this:
(Get-Location).Path
If you want to run sdsSysMain.ps1 from the same directory as the script calling it, change this:
Powershell -noexit ".\sdsSysMain.ps1"
into this:
$scriptPath = Split-Path -Parent $script:MyInvocation.MyCommand.Path
Powershell -NoExit -File "$scriptPath\sdsSysMain.ps1"

Related

'[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ;\UpdateView.exe' is not recognized as the name of a cmdlet

I have the following inside my Power Shell PS1 file, to set Tls12 + call .exe :-
Show-Message -Message "Step 1a: Create groups and adding users to it"
& "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $HelperPath\UpdateView.exe" "true" $Username $Password
If ((Get-Content $ErrorLogFile) -ne $Null) {
Show-Message -Message "Creating group and adding users to it failed" -Type ([MessageType]::Failure)
RevertAll $ScriptDirectory 1
return
}
but i am getting this error:-
& : The term '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
\UpdateView.exe' is not
recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At C:\c\tree\master\cloud\src\deployments\Scripts\Deploy.ps1:352 char:7
+ & "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityPro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ([Net.ServicePoi....UpdateView.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
any advice please?
&, the call operator takes a command / executable name / path only as an argument, not whole statements.
(While it can also accept a script block { ... } containing one or more full statements, there's no need for it here, though you can generally use it to create a child scope).
Simply execute the two statements in sequence:
# Set the protocol (an assignment statement)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Call the executable (a command call).
& $HelperPath\UpdateView.exe" true $Username $Password

Converting Write-Output to an input as command

Basically, I run this application a lot with different launch parameters.
I usually open up PowerShell in the folder of application and write something like this.
.\Name.exe -specific -launch -parameters
The application will start running, so I'm trying to streamline this a little bit and grab the syntax for launching the application from its cfg file.
What I've been trying
function startfunction {
cls
echo "---------------------------------------------------------"
echo ""
echo " Enter Config Name"
echo ""
echo "---------------------------------------------------------"
$name = Read-Host -Prompt 'Enter Config';
Get-Content -Path .\application\configs\$name.cfg |
Write-Output -OutVariable launch;
Start-Process $launch;
}
Contents of $name.cfg:
.\Name.exe -specific -launch -parameters
Results/Error:
Enter Config: Name
.\Name.exe -specific -launch -parameters
Start-Process : Cannot convert 'System.Collections.ArrayList' to the type
'System.String' required by parameter 'FilePath'. Specified method is not
supported.
At C:\PSM0001.ps1:38 char:17
+ Start-Process $launch;
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.StartProcessCommand

Need help in writing EICAR content in a utf8 file using PowerShell

Basically I have file in my Windows machine of UTF8 code. Similar to this path directory and file "G:\这是一\个令人沮". And I am trying to write EICAR string (X5O!P%#AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*) to the file using PowerShell.
For regular file path it is being done like following:
#Base64 of Eicar string
[string] $EncodedEicar = 'WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNULUZJTEUhJEgrSCo='
if (!(Test-Path -Path $FilePath)) {
try {
[byte[]] $EicarBytes = [System.Convert]::FromBase64String($EncodedEicar)
[string] $Eicar = [System.Text.Encoding]::UTF8.GetString($EicarBytes)
Set-Content -Value $Eicar -Encoding ascii -Path $FilePath -Force
}
Tried the following suggestions didnt help :(
Executed powershell.exe "& {Import-Module C:\PROGRA~1\WindowsPowershell\Modules\new_eicar.psm1 -Force -DisableNameChecking ;New-Eicar -Path 'N:\这是一\个令人沮' -FileName 'eicar.com'; if ($?) {exit 0} else {exit 1}}". Result: {'status': 1, 'stderr': '', 'stdout': 'New-Eicar : Cannot validate argument on parameter \'Path\'. The "Test-Path $_ \r\n-PathType \'Container\'" validation script for the argument with value \r\n"N:\\\x8a\xa8T\x91~_\x84,?\\\x84,\xa6\x84\xaf\x0f\x84\xa7\xa7\x91\xfdr" did not return a result of True. Determine why the \r\nvalidation script failed, and then try the command again.\r\nAt line:1 char:116\r\n+ ... ew-Eicar -Path \'N:\\\x8a\xa8T\x91~_\x84,?\\\x84,\xa6\x84\xaf\x0f\x84\xa7\xa7\x91\xfdr\' -FileName \'eicar.com\'; if \r\n($?) {exit ...\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidData: (:) [New-Eicar], ParameterBindingVa \r\n lidationException\r\n + FullyQualifiedErrorId : ParameterArgumentValidationError,New-Eicar\r\n \r\n'}
2017-08-30 23:31:12 DEBUG ssh.py:228 (TB) 10.5.22.4>> 'powershell.exe "& {Import-Module C:\PROGRA~1\WindowsPowershell\Modules\new_eicar.psm1 -Force -DisableNameChecking ;New-Eicar -Path 'N:\这是一\个令人沮' -FileName 'eicar.com'; if ($?) {exit 0} else {exit 1}}"', timeout: 300
2017-08-30 23:31:13 DEBUG ssh.py:306 (TB) 10.5.22.4<< '{
status: 1,
stderr: ,
stdout: New-Eicar : Cannot validate argument on parameter 'Path'. The "Test-Path $_
-PathType 'Container'" validation script for the argument with value
"N:\T~_,?\,r" did not return a result of True. Determine why the
validation script failed, and then try the command again.
At line:1 char:116
+ ... ew-Eicar -Path 'N:\T~_,?\,r' -FileName 'eicar.com'; if
($?) {exit ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Eicar], ParameterBindingVa
lidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,New-Eicar
}'

PowerShell: Change desktop wallpaper on remote computer

I'm trying to modify PS script, but still cant figureout what can be wrong.
Original script:
cp "image.jpg" \\<IP>\C$
$RemoteReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', "<IP>")
$WallPaper= $RemoteReg.OpenSubKey("S-1-5-21-780093305-1579027723-3042286928-500\Control Panel\Desktop",$True)
$WallPaper.SetValue("Wallpaper","C:\image.jpg")
But I need to use variable SIDs.
So I've made this script:
$hostname = Read-Host 'enter hostname of station'
$username = Read-Host 'enter username (login)'
$objUser = New-Object System.Security.Principal.NTAccount($username)
$userSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$SIDandPath = Join-Path $userSID "Control Panel\Desktop"
cp d:\pic1.jpg \\$hostname\c$
$BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( 'Users' , $hostname )
$SubKey = $BaseKey.OpenSubKey($SIDandPath,$true)
$SubKey.SetValue("Wallpaper","d:\pic1.jpg")
And I'm still receiving thi error:
You cannot call a method on a null-valued expression. At
C:\Users\"username"\Scripty\change_remote_wallpaper.ps1:32 char:17
+ $SubKey.SetValue <<<< ("Wallpaper","d:\pic1.jpg")
+ CategoryInfo : InvalidOperation: (SetValue:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas what can be wrong? If I set the value of $SubKey to "1", then I receive this:
"Method invocation failed because [System.Int32] doesn't contain a
method named 'SetValue'."
So I think, that the problem is with "OpenSubKey"
Thanks for any help!!

Issue while executing simple PS script

PS C:> c:\test.ps1
Error:
*The term 'c:\test.ps1' is not recognized as the name of a
spelling of the name, or if a path was included, verify t
At line:1 char:12
+ c:\test.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (c:\test.ps1
+ FullyQualifiedErrorId : CommandNotFoundException*
My Script contains
$letterArray = "a","b","c","d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
Move the script to another(deeper) folder and try again? You can also try
& c:\test.ps1