从批处理文件调用时创建新对象的 Powershell 问题



>我使用NextPVR在电视上录制一些节目,并有一个Powershell脚本,可以将录制的TS文件转换为MP4,用MetaX标记文件,然后打算将文件添加到iTunes。

下一个PVR在录制完成后自动调用一个名为PostProcessing.bat的批处理文件,并传递许多参数。 在此批处理文件中,我有以下命令来调用我的 Powershell 脚本:

::Place this batch file in "C:UsersPublicNPVRScripts" 
@ECHO OFF
cd /d "%~dp0" 
SET parent=%~dp1
IF %parent:~-1%== SET parent=%parent:~0,-1%
:: Call PowerShell script to handle post processing.
powershell -ExecutionPolicy RemoteSigned -File "C:UsersSteveDropboxApplicationsScriptsNVPR.ps1" "%parent%" %1
EXIT
我相信

这个脚本被称为本地系统,我相信这是导致我的主要问题的原因。我有以下函数,当以我自己身份运行时可以完美运行,但在通过批处理文件调用时会失败:

function addToiTunes($path) {
    # get reference to the running iTunes
    $iTunes = New-Object -ComObject iTunes.application
    $LibrarySource = $iTunes.LibrarySource
    foreach ($pList in $LibrarySource.Playlists) {
        if ($pList.Name -eq "Library") {
            $playList = $pList
            Break
        }
    }
    try {
        $playList.AddFile($path)
    } catch {
        return $false
    }
    return $true
}

有没有办法我可以以当前登录用户的身份调用 PS1,而无需使用凭据等,或者我可以将 ComObject 附加到登录用户下运行的进程?

我想你可能在错误的地方搜索你的问题。

批处理文件以及从它们运行的后续 powershell 脚本将接收调用它们的用户的权限。如果您在 Windows 或更高版本上,您可能会受到 Windows 安全性的打击,但您可以通过右键单击批处理并点击"以管理员身份运行"或打开管理员 cmd 提示符并从那里运行批处理来避免这种情况。

将其

作为其他进程打开的唯一其他方法是将批处理文件计划到任务并将 runas user 设置为 SYSTEM。但就像我说的,如果您以具有适当权限的用户身份登录,您应该没有任何问题。

好的,所以我几乎回答了我自己的问题。 我最终创建了另外几个脚本,一个用于创建安全密码并将其存储到PC:

$password = Read-Host "Please enter a password to be encrypted:" -AsSecureString
$path = Read-Host "Please enter a path where pwd.txt is to be stored:"
If ($path[-1] -ne "") {
    $path += ""
}
$path += "pwd.txt"
ConvertFrom-SecureString $password | Out-File $path

第二个可以从文件中获取凭据,并启动传递给它的脚本(Params 的处理尚未工作,但我越来越近了(:

# Get parameter credentials
$username = $args[0]
$passwordPath = $args[1]
$scriptToRun = $args[2]
$param1 = $args[3]
$param2 = $args[4]
$param3 = $args[5]
$param4 = $args[6]
$param5 = $args[7]
$param6 = $args[8]
# Collect arguments into a single array.
$arguments = @()
if ($param1 -ne $null) {$arguments += $param1}
if ($param2 -ne $null) {$arguments += $param2}
if ($param3 -ne $null) {$arguments += $param3}
if ($param4 -ne $null) {$arguments += $param4}
if ($param5 -ne $null) {$arguments += $param5}
if ($param6 -ne $null) {$arguments += $param6}
# Set credentials and launch passed script.
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString (Get-Content $passwordPath)))
Invoke-Command -FilePath $scriptToRun -Credential $cred -ComputerName localhost -ArgumentList $arguments

最新更新