如果为真,Powershell打开Ps1

时间:2014-11-20 10:29:00

标签: powershell powershell-v2.0

我在这里做错了什么?我试图打开第二个PS1文件,如果为True但抛出错误(如下所示)。

#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... :(" 
}

这是我得到的错误:

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

1 个答案:

答案 0 :(得分:1)

错误是不言自明的。脚本在当前工作目录中找不到sdsSysMain.ps1。您可以在脚本中输出当前工作目录,如下所示:

(Get-Location).Path

如果要从调用它的脚本所在的目录运行sdsSysMain.ps1,请更改:

Powershell -noexit ".\sdsSysMain.ps1"

进入这个:

$scriptPath = Split-Path -Parent $script:MyInvocation.MyCommand.Path
Powershell -NoExit -File "$scriptPath\sdsSysMain.ps1"