简体   繁体   中英

Powershell issue creating New-Object when called from a batch file

I use NextPVR to record some shows off the telly, and have a powershell script which converts the recorded TS file into MP4, tags the file with MetaX and then is intended to add the file to iTunes.

NextPVR upon completion of recording automatically calls a batch file called PostProcessing.bat, and passes a number of parameters. Inside this Batch file, I have the following command to call my Powershell script:

::Place this batch file in "C:\Users\Public\NPVR\Scripts" 
@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:\Users\Steve\Dropbox\Applications\Scripts\NVPR.ps1" "%parent%" %1
EXIT

This script I believe is called as Local System, which I believe is causing my main issue. I have the following function, which works perfectly when run as myself, but fails when called via the batch file:

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
}

Is there a way I can either call the PS1 as the current logged in user without having to use credentials etc, or can I attach the ComObject to a process running under the logged in user?

I think you might be searching for your problem in the wrong place.

Batch files, and the subsequent powershell scripts that run from them, receive the permissions of the user calling them. If your on windows or up, you maybe getting hit by windows security, but you can avoid this by right clicking the batch and hitting "run as administrator" or opening up an admin cmd prompt and running the batch from there.

The only other way you can get this to open as a different process is to schedule the batch file to a task and set runas user to SYSTEM. But like I said, if your logged on as a user with appropriate permissions, you should not have any problems.

Okay, so I've pretty much answered my own question. I ended up creating another couple of scripts, one to create a secure password and store it to the 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

And a second which can get credentials from file, and launch a script which is passed to it (the handling of Params is not yet working, but I'm getting closer):

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM