在ISE中运行此代码。
Push-Location -Path $(Split-Path -Parent $myInvocation.MyCommand.Path)
Get-Location
$file = '.\ex.txt'
$reader = New-Object System.IO.StreamReader($file)
在Console中运行相同的代码失败。我错过了什么?
PS H:\src\powershell> .\ccount.ps1
Path
----
H:\src\powershell
New-Object : Exception calling ".ctor" with "1" argument(s): "Could not find file
'C:\src\powershell\ex.txt'."
At H:\src\powershell\ccount.ps1:9 char:11
+ $reader = New-Object System.IO.StreamReader($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands
.NewObjectCommand
这与建议的重复
有何不同另一个问题/答案确实解释了PowerShell在这种情况下失败的原因。但是,它没有提供任何关于为何在ISE中有效的提示。这似乎是Console和ISE主机之间的显着差异。
我在Windows 7 Enterprise SP1上运行PSVersion 5.0.10586.117。
答案 0 :(得分:1)
function drawChart(data) {
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'FiveRP',
data: [],
backgroundColor: "rgba(75,192,192,0.4)",
fill: false,
lineTension: 0.1,
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt'
}, {
label: 'GTALife',
data: [],
backgroundColor: "rgba(148,0,211,0.4)",
fill: false,
lineTension: 0.1,
borderColor: "rgba(148,0,211,1)",
borderCapStyle: 'butt'
}, {
label: 'GermanV',
data: [],
backgroundColor: "rgba(255,165,0,0.4)",
fill: false,
lineTension: 0.1,
borderColor: "rgba(255,165,0,1)",
borderCapStyle: 'butt'
}]
}
});
}
答案 1 :(得分:0)
只需使用:
Push-Location $PSScriptRoot
只要您使用最新的PowerShell版本(v3 +),它就会在两种情况下都有效。
答案 2 :(得分:0)
答案似乎是Push-Location
不会更改控制台主机中的[Environment] :: CurrentDirectory。确实会在ISE中更改它。
PS 09:02 \\SRV1\SH1\home\pwatson2 H:\
>Push-Location H:\src\t
PS 09:02 \\SRV1\SH1\home\pwatson2 H:\src\t
>Get-Location
Path
----
H:\src\t
PS 09:02 \\SRV1\SH1\home\pwatson2 H:\src\t
>Write-Host ([Environment]::CurrentDirectory)
C:\Windows\System32\WindowsPowerShell\v1.0