I have been uninstalling software via below my powerscript. But I have an issue inside powershell script. When attempting to run any software then I got the following the error message.
My error message :
PS C:\Windows\system32> doRemoveMSI -msi "msiexec.exe" -arguments "/x {EFF8AB9B-9551-4994-9FD2-D40A5A2E9B31}"
Index was outside the bounds of the array.
At line:123 char:65
+ ... Name doRemoveMSI: $msi, a0 = '$($arguments[0]),a1='$($arguments[1])'"
+ ~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException
Remove-Symantec doRemoveMSI: msiexec.exe, a0 = '/x {EFF8AB9B-9551-4994-9FD2-D40A5A2E9B31},a1=''
Remove-Symantec doRemoveMSI: successful result is System.Diagnostics.Process, exitcode = 0
Remove-Symantec doRemoveMSI: success, return = 0
Remove-Symantec doRemoveMSI: exit
0
Here is my script:
[CmdletBinding()]
##
## rem-sep.ps1
##
[String] $logname = 'Application'
[String] $source = 'IntuneWinApp'
[String] $pName = 'Remove-Symantec'
Set-StrictMode -Version Latest
Set-Variable evtError 1
Set-Variable evtWarning 2
Set-Variable evtInformation 4
Set-Variable evtAuditSuccess 8
Set-Variable evtAuditFailure 16
function NewMyEventlogSource
{
Param
(
[string] $source,
[string] $logname
)
[bool] $success = $false
try
{
$success = [System.Diagnostics.EventLog]::SourceExists( $source )
}
catch [ System.Security.SecurityException ]
{
## we aren't running as a privileged account so we
## couldn't search the security log (and maybe others).
## we have no choice but to try to create the $source.
## however, that will probably also fail.
}
if( -not $success )
{
try
{
[System.Diagnostics.EventLog]::CreateEventSource( $source, $logname )
$success = $true
}
catch
{
$e = $_
Write-Warning "Cannot create event source '$source', error $e"
}
}
$success
}
function WriteMyEventlog
{
Param
(
[ValidateNotNullOrEmpty()]
[String] $logname,
[ValidateNotNullOrEmpty()]
[String] $source,
[ValidateNotNullOrEmpty()]
[String] $message,
[ValidateSet( 1, 2, 4, 8, 16 )]
[Int] $type = $evtInformation,
[ValidateRange( 1, 65535 )]
[Int] $eventId = 1,
[ValidateRange( 0, 32767 )]
[Int16] $category = 0,
[Bool] $override = $false
)
<#
if( [String]::IsNullOrEmpty( $message ) )
{
$ex = New-Object System.ArgumentException( "Message parameter may not be null or empty" )
throw $ex
}
#>
if( NewMyEventlogSource -source $source -logname $logname )
{
[System.Diagnostics.EventLog]::WriteEntry(
$source,
$message,
$type, ## [System.Diagnostics.EventLog]::EventLogEntryType enum
$eventId,
$category
)
}
elseif( $override )
{
[System.Diagnostics.EventLog]::WriteEntry(
'Application', ## 'Application' always exists in the Application log
$message,
$type,
$eventId,
$category
)
}
}
function log
{
$str = $args -join ' '
Write-Host $str
WriteMyEventlog -logname $logname -source $source `
-message $str `
-type $evtInformation `
-eventid 1 `
-category 0 `
-override $true
}
function logE
{
$str = $args -join ' '
Write-Host $str
WriteMyEventlog -logname $logname -source $source `
-message $str `
-type $evtError `
-eventid 99 `
-category 0 `
-override $true
}
function doRemoveMSI
{
Param
(
[String] $msi,
[String[]] $arguments
)
log "$pName doRemoveMSI: $msi, a0 = '$( $arguments[ 0 ] )', a1 = '$( $arguments[ 1 ] )'"
<#
$fileExists = Test-Path $arguments[ 1 ] -PathType Leaf
if( -not $fileExists )
{
logE "$pName doMSI: file doesn't exist '$( $arguments[ 1 ] )'"
}
#>
$result = Start-Process -Wait -Verb RunAs -FilePath $msi -ArgumentList $arguments -PassThru -WorkingDirectory $pwd.Path
if( $? )
{
$return = 0
if( $null -eq $result )
{
log "$pName successful result from Start-Process is null"
}
else
{
if( $result -is [System.Diagnostics.Process] )
{
$return = $result.ExitCode
log "$pName doRemoveMSI: successful result is System.Diagnostics.Process, exitcode = $return"
if( $return -eq 1619 )
{
logE "$pName doRemoveMSI: a lie! msiexec error 1619 on '$( $arguments[ 0 ] )' '$( $arguments[ 1 ] )'"
}
}
else
{
$return = $result.ToString()
log "$pName doRemoveMSI: successful result is type $( $result.GetType().FullName )"
}
}
log "$pName doRemoveMSI: success, return = $return"
}
else
{
$e = $error[ 0 ]
logE "$pName doRemoveMSI: error $e"
$return = -1
}
log "$pName doRemoveMSI: exit"
return $return
}
##WriteMyEventlog $logname $source $message $type $eventId $category $override