How to run a Powershell script from the command line and pass a directory as a parameter

前端 未结 6 1111
醉梦人生
醉梦人生 2020-12-24 04:40
PowerShell -Command .\\Foo.ps1
  • Foo.ps1:

    
    
            
6条回答
  •  青春惊慌失措
    2020-12-24 05:11

    Change your code to the following :

    Function Foo($directory)
        {
            echo $directory
        }
    
        if ($args.Length -eq 0)
        {
            echo "Usage: Foo "
        }
        else
        {
            Foo([string[]]$args)
        }
    

    And then invoke it as:

    powershell -ExecutionPolicy RemoteSigned -File "c:\foo.ps1" "c:\Documents and Settings" "c:\test"

提交回复
热议问题