WhoAmI 文件夹下脚本Get-LocalReuslt.ps1
Function Get-MyLocation{
write-host "Who am I in local function "
write-host "PSCommandPath = psScriptRoot "
write-host "myInvocation.myCommand.name = myinvocation.myCommand.name)"
write-host "myInvocation.myCommand.path = MyInvocation.MyCommand.Path)"
write-host "myInvocation.scriptName = myinvocation.scriptName)"
write-host "myInvocation.psScriptRoot = myinvocation.psScriptRoot)"
write-host "myInvocation.psCommandPath = myinvocation.psCommandPath)"
Get-MyLocationInNest
write-host "Who am I in sub folder nested cmdlet"
Get-MyLocationInSubFunction
}
Function Get-MyLocationInNest{
write-host "Who am I in local nested cmdlet"
write-host "PSCommandPath = psScriptRoot "
write-host "myInvocation.myCommand.name = myinvocation.myCommand.name)"
write-host "myInvocation.myCommand.path = MyInvocation.MyCommand.Path)"
write-host "myInvocation.scriptName = myinvocation.scriptName)"
write-host "myInvocation.psScriptRoot = myinvocation.psScriptRoot)"
write-host "myInvocation.psCommandPath = myinvocation.psCommandPath)"
}
. C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResultInFunction.ps1
write-host "Who am I in local cmdlet"
write-host "PSCommandPath = psScriptRoot "
write-host "myInvocation.myCommand.name = myinvocation.myCommand.name)"
write-host "myInvocation.myCommand.path = MyInvocation.MyCommand.Path)"
write-host "myInvocation.scriptName = myinvocation.scriptName)"
write-host "myInvocation.psScriptRoot = myinvocation.psScriptRoot)"
write-host "myInvocation.psCommandPath = myinvocation.psCommandPath)"
write-host "call local function"
Get-MyLocation
write-host "call subfolder function"
Get-MyLocationInSubFunction
write-host "run sub script "
& C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResult.ps1
SufFolder文件夹下脚本
1.Get-SonResult.ps1
write-host "Who am I in subfolder " -ForegroundColor Yellow
write-host "PSCommandPath = psScriptRoot "
write-host "myInvocation.myCommand.name = myinvocation.myCommand.name)"
write-host "myInvocation.myCommand.path = MyInvocation.MyCommand.Path)"
write-host "myInvocation.scriptName = myinvocation.scriptName)"
write-host "myInvocation.psScriptRoot = myinvocation.psScriptRoot)"
write-host "myInvocation.psCommandPath = myinvocation.psCommandPath)"
& C:\Users\Fei\Desktop\WhoAmI\subFolder\grandson\Get-GrandSonResult.ps1
- Get-SonResultInFunction.ps1
Function Get-MyLocationInSubFunction{
Write-Host "Who am I when I was a function in sub folder" -ForegroundColor Yellow
write-host "PSCommandPath = $PSCommandPath"
write-host "psScriptRoot = $psScriptRoot "
write-host "myInvocation.myCommand.name = $($myinvocation.myCommand.name)"
write-host "myInvocation.myCommand.path = $MyInvocation.MyCommand.Path"
write-host "myInvocation.scriptName = $($myinvocation.scriptName)"
write-host "myInvocation.psScriptRoot = $($myinvocation.psScriptRoot)"
write-host "myInvocation.psCommandPath = $($myinvocation.psCommandPath)"
}
3.GrandSon下脚本Get-GrandSonResult.ps1
write-host "Who am I in grandson folder"
write-host "PSCommandPath = psScriptRoot "
write-host "myInvocation.myCommand.name = myinvocation.myCommand.name)"
write-host "myInvocation.myCommand.path = MyInvocation.MyCommand.Path)"
write-host "myInvocation.scriptName = myinvocation.scriptName)"
write-host "myInvocation.psScriptRoot = myinvocation.psScriptRoot)"
write-host "myInvocation.psCommandPath = myinvocation.psCommandPath)"
目录结构
目录: C:\Users\Fei\Desktop\WhoAmI
Mode LastWriteTime Length Name
d----- 2022/4/26 16:01 subFolder
-a---- 2022/4/26 15:51 2524 Get-LocalReuslt.ps1
目录: C:\Users\Fei\Desktop\WhoAmI\subFolder
Mode LastWriteTime Length Name
d----- 2022/4/26 15:11 grandson
-a---- 2022/4/26 15:23 763 Get-SonResult.ps1
-a---- 2022/4/26 15:27 777 Get-SonResultInFunction.ps1
目录: C:\Users\Fei\Desktop\WhoAmI\subFolder\grandson
Mode LastWriteTime Length Name
-a---- 2022/4/26 15:11 677 Get-GrandSonResult.ps1
运行Get-LocalReuslt.ps1,返回结果如下:
Who am I in local cmdlet
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.myCommand.name = Get-LocalReuslt.ps1
myInvocation.myCommand.path = C:\Users\Fei\Desktop\WhoAmI
myInvocation.scriptName =
myInvocation.psScriptRoot =
myInvocation.psCommandPath =
call local function
Who am I in local function
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.myCommand.name = Get-MyLocation
Split-Path : 无法将参数绑定到参数“Path”,因为该参数是空值。
所在位置 C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1:6 字符: 61
- ... ocation.myCommand.path = MyInvocation.MyCommand.Path)"
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidData: (:) [Split-Path],ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
myInvocation.myCommand.path =
myInvocation.scriptName = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
myInvocation.psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.psCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
Who am I in local nested cmdlet
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.myCommand.name = Get-MyLocationInNest
Split-Path : 无法将参数绑定到参数“Path”,因为该参数是空值。
所在位置 C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1:23 字符: 61
- ... ocation.myCommand.path = MyInvocation.MyCommand.Path)"
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidData: (:) [Split-Path],ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
myInvocation.myCommand.path =
myInvocation.scriptName = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
myInvocation.psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.psCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
Who am I in sub folder nested cmdlet
Who am I when I was a function in sub folder
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResultInFunction.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI\subFolder
myInvocation.myCommand.name = Get-MyLocationInSubFunction
myInvocation.myCommand.path = System.Management.Automation.InvocationInfo.MyCommand.Path
myInvocation.scriptName = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
myInvocation.psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.psCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
call subfolder function
Who am I when I was a function in sub folder
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResultInFunction.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI\subFolder
myInvocation.myCommand.name = Get-MyLocationInSubFunction
myInvocation.myCommand.path = System.Management.Automation.InvocationInfo.MyCommand.Path
myInvocation.scriptName = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
myInvocation.psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.psCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
run sub script
Who am I in subfolder
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResult.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI\subFolder
myInvocation.myCommand.name = Get-SonResult.ps1
myInvocation.myCommand.path = C:\Users\Fei\Desktop\WhoAmI\subFolder
myInvocation.scriptName = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
myInvocation.psScriptRoot = C:\Users\Fei\Desktop\WhoAmI
myInvocation.psCommandPath = C:\Users\Fei\Desktop\WhoAmI\Get-LocalReuslt.ps1
Who am I in grandson folder
PSCommandPath = C:\Users\Fei\Desktop\WhoAmI\subFolder\grandson\Get-GrandSonResult.ps1
psScriptRoot = C:\Users\Fei\Desktop\WhoAmI\subFolder\grandson
myInvocation.myCommand.name = Get-GrandSonResult.ps1
myInvocation.myCommand.path = C:\Users\Fei\Desktop\WhoAmI\subFolder\grandson
myInvocation.scriptName = C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResult.ps1
myInvocation.psScriptRoot = C:\Users\Fei\Desktop\WhoAmI\subFolder
myInvocation.psCommandPath = C:\Users\Fei\Desktop\WhoAmI\subFolder\Get-SonResult.ps1