Using credentials on ScriptBlock causing COM error

2019-09-18 04:57发布

问题:

Okay, I have a proof of concept script that looks like this for connecting to iTunes:

$username = Read-Host "Please enter your userid for the local machine"
$password = Read-Host "Please enter a password to be encrypted" -AsSecureString
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,$password)

Invoke-Command -ScriptBlock { 
    $iTunes = New-Object -ComObject iTunes.application
    $itunes | gm
} -ComputerName localhost -Credential $cred

However it is returning the following error:

Retrieving the COM class factory for component with CLSID {DC0C2640-1415-4644-875C-6F4D769839BA} failed due to the following error: 80080005.
    + CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
    + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

Where it gets interesting, is if I remove -ComputerName localhost -Credential $cred from the script block, it works fine:

   TypeName: System.__ComObject#{9dd6680b-3edc-40db-a771-e6fe4832e34a}

Name                               MemberType            Definition                                                
----                               ----------            ----------                                                
Authorize                          Method                void Authorize (int, Variant, string)                     
BackTrack                          Method                void BackTrack ()                                         
CheckVersion                       Method                bool CheckVersion (int, int)                              
ConvertFile                        Method                IITOperationStatus ConvertFile (string)                   
ConvertFile2                       Method                IITConvertOperationStatus ConvertFile2 (string)           
ConvertFiles                       Method                IITOperationStatus ConvertFiles (Variant)                 
ConvertFiles2                      Method                IITConvertOperationStatus ConvertFiles2 (Variant)         
ConvertTrack                       Method                IITOperationStatus ConvertTrack (Variant)                 
ConvertTrack2                      Method                IITConvertOperationStatus ConvertTrack2 (Variant)         
ConvertTracks                      Method                IITOperationStatus ConvertTracks (Variant)                
ConvertTracks2                     Method                IITConvertOperationStatus ConvertTracks2 (Variant)        
CreateEQPreset                     Method                IITEQPreset CreateEQPreset (string)                       
CreateFolder                       Method                IITPlaylist CreateFolder (string)                         
CreateFolderInSource               Method                IITPlaylist CreateFolderInSource (string, Variant)        
CreatePlaylist                     Method                IITPlaylist CreatePlaylist (string)                       
CreatePlaylistInSource             Method                IITPlaylist CreatePlaylistInSource (string, Variant)      
FastForward                        Method                void FastForward ()                                       
GetITObjectByID                    Method                IITObject GetITObjectByID (int, int, int, int)            
GetITObjectPersistentIDs           Method                void GetITObjectPersistentIDs (Variant, int, int)         
GetPlayerButtonsState              Method                void GetPlayerButtonsState (bool, ITPlayButtonState, bool)
GotoMusicStoreHomePage             Method                void GotoMusicStoreHomePage ()                            
NextTrack                          Method                void NextTrack ()                                         
OpenURL                            Method                void OpenURL (string)                                     
Pause                              Method                void Pause ()                                             
Play                               Method                void Play ()                                              
PlayerButtonClicked                Method                void PlayerButtonClicked (ITPlayerButton, int)            
PlayFile                           Method                void PlayFile (string)                                    
PlayPause                          Method                void PlayPause ()                                         
PreviousTrack                      Method                void PreviousTrack ()                                     
Quit                               Method                void Quit ()                                              
Resume                             Method                void Resume ()                                            
Rewind                             Method                void Rewind ()                                            
SetOptions                         Method                void SetOptions (int)                                     
Stop                               Method                void Stop ()                                              
SubscribeToPodcast                 Method                void SubscribeToPodcast (string)                          
UpdateIPod                         Method                void UpdateIPod ()                                        
UpdatePodcastFeeds                 Method                void UpdatePodcastFeeds ()                                
CanSetShuffle                      ParameterizedProperty bool CanSetShuffle (Variant) {get}                        
CanSetSongRepeat                   ParameterizedProperty bool CanSetSongRepeat (Variant) {get}                     
ITObjectPersistentIDHigh           ParameterizedProperty int ITObjectPersistentIDHigh (Variant) {get}              
ITObjectPersistentIDLow            ParameterizedProperty int ITObjectPersistentIDLow (Variant) {get}               
AppCommandMessageProcessingEnabled Property              bool AppCommandMessageProcessingEnabled () {get} {set}    
BrowserWindow                      Property              IITBrowserWindow BrowserWindow () {get}                   
ConvertOperationStatus             Property              IITConvertOperationStatus ConvertOperationStatus () {get} 
CurrentEncoder                     Property              IITEncoder CurrentEncoder () {get} {set}                  
CurrentEQPreset                    Property              IITEQPreset CurrentEQPreset () {get} {set}                
CurrentPlaylist                    Property              IITPlaylist CurrentPlaylist () {get}                      
CurrentStreamTitle                 Property              string CurrentStreamTitle () {get}                        
CurrentStreamURL                   Property              string CurrentStreamURL () {get}                          
CurrentTrack                       Property              IITTrack CurrentTrack () {get}                            
CurrentVisual                      Property              IITVisual CurrentVisual () {get} {set}                    
Encoders                           Property              IITEncoderCollection Encoders () {get}                    
EQEnabled                          Property              bool EQEnabled () {get} {set}                             
EQPresets                          Property              IITEQPresetCollection EQPresets () {get}                  
EQWindow                           Property              IITWindow EQWindow () {get}                               
ForceToForegroundOnDialog          Property              bool ForceToForegroundOnDialog () {get} {set}             
FullScreenVisuals                  Property              bool FullScreenVisuals () {get} {set}                     
LibraryPlaylist                    Property              IITLibraryPlaylist LibraryPlaylist () {get}               
LibrarySource                      Property              IITSource LibrarySource () {get}                          
LibraryXMLPath                     Property              string LibraryXMLPath () {get}                            
Mute                               Property              bool Mute () {get} {set}                                  
PlayerPosition                     Property              int PlayerPosition () {get} {set}                         
PlayerState                        Property              ITPlayerState PlayerState () {get}                        
SelectedTracks                     Property              IITTrackCollection SelectedTracks () {get}                
SoundVolume                        Property              int SoundVolume () {get} {set}                            
SoundVolumeControlEnabled          Property              bool SoundVolumeControlEnabled () {get}                   
Sources                            Property              IITSourceCollection Sources () {get}                      
Version                            Property              string Version () {get}                                   
Visuals                            Property              IITVisualCollection Visuals () {get}                      
VisualsEnabled                     Property              bool VisualsEnabled () {get} {set}                        
VisualSize                         Property              ITVisualSize VisualSize () {get} {set}                    
Windows                            Property              IITWindowCollection Windows () {get}                      

Does anyone know why the credentials on the script block could be causing this problem?

The reason is I'm working on a way to run some iTunes tasks, when called from a LOCAL SYSTEM service, so in a way, I'm trying to elevate the call back to the logged-in user.