Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从与powershell脚本相同的目录安装.MSI文件,而不设置静态文件路径_Powershell_Installation_Windows Installer - Fatal编程技术网

从与powershell脚本相同的目录安装.MSI文件,而不设置静态文件路径

从与powershell脚本相同的目录安装.MSI文件,而不设置静态文件路径,powershell,installation,windows-installer,Powershell,Installation,Windows Installer,我有一个我们在Powershell中使用的脚本,但是我需要该脚本能够找到动态安装应用程序所需的文件,因为用户可以将文件夹复制到其计算机上的任何文件夹。目前我有下面的代码集,我想我的问题是,脚本是否与安装文件位于同一文件夹中。我如何让powershell只在运行它的目录中查找安装文件 $Install = "C:\Other Folder\File.msi" $InstallArg = "/quite /norestart" Start-Process ' -FilePath

我有一个我们在Powershell中使用的脚本,但是我需要该脚本能够找到动态安装应用程序所需的文件,因为用户可以将文件夹复制到其计算机上的任何文件夹。目前我有下面的代码集,我想我的问题是,脚本是否与安装文件位于同一文件夹中。我如何让powershell只在运行它的目录中查找安装文件

$Install = "C:\Other Folder\File.msi"
$InstallArg = "/quite /norestart"
     Start-Process '
     -FilePath $Install '
     -ArgumentList $InstallArg '
     -PassThru | Wait-Process
任何帮助都将不胜感激。多谢各位


更新时,我发现我必须在脚本所在的目录中。但是,由于我们必须使用管理员凭据运行ISE,因此无论我是否告诉它打开脚本,它都会自动默认为C:\Windows\System32,因为powershell正在查找目录。如果是这种情况,我如何告诉它查看脚本的位置,以便找到所需的文件?

定义一个相对路径:

$Install = ".\File.msi"
如果您没有在存储脚本本身和可执行文件的目录中运行脚本,则必须确定它的绝对路径。使用PowerShell 3+可以使用
$PSScriptRoot
变量轻松确定脚本存储的目录。您可以这样定义
$Install
变量:

$Install = Join-Path -Path $PSScriptRoot -ChildPath "File.msi"

定义相对路径:

$Install = ".\File.msi"
如果您没有在存储脚本本身和可执行文件的目录中运行脚本,则必须确定它的绝对路径。使用PowerShell 3+可以使用
$PSScriptRoot
变量轻松确定脚本存储的目录。您可以这样定义
$Install
变量:

$Install = Join-Path -Path $PSScriptRoot -ChildPath "File.msi"

我在下面找到了我的答案,这是我如何使它与我们目前的情况相适应的。谢谢托马斯的帮助

$ScriptLocation = Get-ChildItem -Path C:\Users -Filter Untitled2.ps1 -Recurse | Select Directory
cd $ScriptLocation.Directory
$Install = ".\Install.msi"
$InstallArg = "/quite /norestart"
Start-Process '
-FilePath $Install '
-ArgumentList $InstallArg '
-PassThru | Wait-Process

我在下面找到了我的答案,这是我如何使它与我们目前的情况相适应的。谢谢托马斯的帮助

$ScriptLocation = Get-ChildItem -Path C:\Users -Filter Untitled2.ps1 -Recurse | Select Directory
cd $ScriptLocation.Directory
$Install = ".\Install.msi"
$InstallArg = "/quite /norestart"
Start-Process '
-FilePath $Install '
-ArgumentList $InstallArg '
-PassThru | Wait-Process

非常感谢大家的帮助!我偶然发现了完美的解决方案

# Set active path to script-location:
$path = $MyInvocation.MyCommand.Path
if (!$path) {
    $path = $psISE.CurrentFile.Fullpath
}
if ($path) {
    $path = Split-Path $path -Parent
}
Set-Location $path

非常感谢大家的帮助!我偶然发现了完美的解决方案

# Set active path to script-location:
$path = $MyInvocation.MyCommand.Path
if (!$path) {
    $path = $psISE.CurrentFile.Fullpath
}
if ($path) {
    $path = Split-Path $path -Parent
}
Set-Location $path

我已经尝试过了,但出现了一个错误,表明无法找到该文件。我更新了我对该案例的回答,即您正在任何您想要的工作目录中运行脚本。哦,好的,这样做更有意义,我们的问题是我们的服务台团队必须使用不同的凭据以管理员身份打开powershell ISE,因此Powershell默认为C:\WIndows\System32,这就是我之前收到错误的原因。我将尝试这种方式并报告我的发现!非常感谢。我已经尝试过了,但出现了一个错误,表明无法找到该文件。我更新了我对该案例的回答,即您正在任何您想要的工作目录中运行脚本。哦,好的,这样做更有意义,我们的问题是我们的服务台团队必须使用不同的凭据以管理员身份打开powershell ISE,因此Powershell默认为C:\WIndows\System32,这就是我之前收到错误的原因。我将尝试这种方式并报告我的发现!非常感谢。这在某些情况下可能有效。重命名脚本后,或者脚本未存储在
C:\Users
下,或者存在多个名为
Untitled2.ps1
的文件时,脚本将失败。总之相当危险。我更新了你问题的答案,你可能更喜欢这种方式。这在某些情况下可能有效。重命名脚本后,或者脚本未存储在
C:\Users
下,或者存在多个名为
Untitled2.ps1
的文件时,脚本将失败。总之相当危险。我为你的问题更新了我的答案,你可能更喜欢这样。