I am trying to get some powershell scripts written by IBM to work on my IEM instance. I have tried serval variations to get the scripts to run, but nothing seems to work.
Here is my current fixlet.
if {name of operating system as lowercase contains “win” and architecture of operating system = “x86”}
regset “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”=“Unrestricted”
//64bit
else
regset “HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”=“Unrestricted”
endif
if {name of operating system as lowercase contains “win” and architecture of operating system = “x86”}
waithidden cmd.exe /C “powershell.exe” scriptsrunner.ps1
else
waithidden cmd.exe /C “C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe” scriptsrunner.ps1
endif
It shows that it completes successfully, but it never actually runs the powershell script.
Here is the actual script that I am trying to get to run. This scripts also calls serveral others depending on what it finds.
Any help would be greatly appreciated.
#************* Begin Standard Header - Do not add comments here** ***************
#
# Licensed Materials - Property of IBM
#
# Restricted Materials of IBM
#
# 5724-N55
#
# © COPYRIGHT IBM CORP. 2013. All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# **************************** End Standard Header *******************************
#files
$ASDTEMPDIR="%windir%\temp\taddm7.3\asd"
# Resolve ASD path if include system enviroment variables
if($ASDTEMPDIR -match “%[\w-]+%”){
$newPath = New-Object System.Text.StringBuilder
$path=$ASDTEMPDIR.Split("")
$index=0
$path |foreach{
$index++
if($_ -match “%[\w-]+%”){
$resolvedPath=[System.Environment]::ExpandEnvironmentVariables($_)
$newPath.append("$resolvedPath")|Out-Null
}
else{
$newPath.append("$_")|Out-Null
}
if($index -lt $path.Count){
$newPath.append("")|Out-Null
}
**}**
**$ASDTEMPDIR=$newPath.ToString()**
}
if(!(Test-Path -Path $ASDTEMPDIR)){
New-Item -Path “$ASDTEMPDIR” -type directory -ErrorAction SilentlyContinue -ErrorVariable err| Out-Null
if($err){
Write-Output "Unable to create $ASDTEMPDIR directory : $err"
return
}
}
$MAIN="$ASDTEMPDIR\tmpoutput.txt"
$ALLERRORS="$ASDTEMPDIR\allerrors.txt"
$ERRORFILE=“output.txt”
$hostname=gwmi -Namespace root\CIMV2 Win32_ComputerSystem -ErrorAction SilentlyContinue | Select Name
$asdFile=“taddmasd-”+$hostname.Name+"-"+(Get-Date -Format “yyMMddHHmm”)
$OUTPUTDIR="$ASDTEMPDIR$asdFile" ## temporary
## if exist, remove old allerrors.txt file
if(Test-Path $ALLERRORS){
Remove-Item $ALLERRORS
}
#########################################
function createItem($name, $cmd, $data, $err){
$UnicodeBomEncoding = New-Object System.Text.UnicodeEncoding($false, $true)
$fileMain=New-Object System.IO.StreamWriter -ArgumentList $MAIN,$false,$UnicodeBomEncoding
$fileMain.WriteLine(“TADDM_DATA_NAME”)
$fileMain.WriteLine($name)
$fileMain.WriteLine(“TADDM_DATA_TYPE”)
$fileMain.WriteLine(“COMMAND”)
$fileMain.WriteLine(“TADDM_DATA_COMMAND”)
$fileMain.WriteLine($cmd)
$fileMain.WriteLine(“TADDM_DATA_VALUE”)
foreach($item in $data){
if($item){
$fileMain.WriteLine($item)
}
else{
$fileMain.WriteLine("")
}
}
$fileMain.WriteLine(“TADDM_DATA_EXITCODE”)
if($err){
$fileMain.WriteLine(“1”)
}
else{
$fileMain.WriteLine(“0”)
}
$fileMain.WriteLine(“TADDM_DATA_ERROR”)
if($err){
$fileMain.WriteLine($err)
}
else{
$fileMain.WriteLine("")
}
$fileMain.WriteLine(“TADDM_DATA_END”)
$fileMain.close()}
function runCmd($name, $cmd){
$EC=0
$ErrorActionPreference='Stop’
try{
$data=Invoke-Expression “$cmd”
}catch {
##terminating error, command failedD7ominika
$err=$error[0]
$EC=1
}
$ErrorActionPreference='Continue’
createItem $name $cmd $data $err
reportErrors $err
}
function logError($name, $err){
$data=""
$cmd=""
$EC=1
createItem $name $cmd $data $err
reportErrors $err
}
function reportErrors($err){
if($err){
$UnicodeBomEncoding = New-Object System.Text.UnicodeEncoding($false, $true)
$fileAllErrors=New-Object System.IO.StreamWriter -ArgumentList $ALLERRORS,$true,$UnicodeBomEncoding
$fileIn = New-Object -typename System.IO.StreamReader -argumentlist $MAIN
$fileAllErrors.WriteLine($fileIn.ReadToEnd())
$fileIn.close()
$fileAllErrors.close()
}
}
#########################################
function runSensorScript ($sensor, $script){
Write-Output “[$sensor, $script]”
$ERRORFILEPATH="${OUTPUTDIR}$sensor$ERRORFILE"
$EC=0
**runCmd "executeMainScript" "powershell.exe -noprofile -file .\$sensor\$script"**
**if($EC -eq 1){**
**return**
**}**
**## find scripts number to run**
**$commonContent=[IO.File]::ReadAllText($MAIN)**
**$pattern='(?s)^.+(TADDM_DATA_VALUE)(.*)(TADDM_DATA_EXITCODE).+$'**
**if($commonContent -match $pattern){**
**$scriptInfo=$Matches[2].Trim().Split("`n")**
**$list=New-Object System.Collections.ArrayList(,$scriptInfo)**
**}**
**else{**
**Write-Output "Unexpected executeMainScript output for $script"**
**return**
**}**
**[int]$scriptsCount=$list.Count/3**
**if($scriptsCount -gt 0){**
**New-Item -Path "$OUTPUTDIR\$sensor" -type directory -ErrorAction SilentlyContinue -ErrorVariable err| Out-Null**
**if($err){**
**Write-Output "Unable to create $OUTPUTDIR\$sensor directory"**
**return**
**}**
**Copy-Item "$sensor\*" "$OUTPUTDIR\$sensor" -ErrorAction SilentlyContinue -ErrorVariable err**
**if($err){**
**Write-Output "Unable to copy scripts to $OUTPUTDIR\$sensor directory"**
**return**
**}**
**}**
**$index=0**
**while($index -lt $scriptsCount){**
**$index++**
**$script=$list.get_Item(0).Replace('SCRIPT:','').Trim()**
**$user=$list.get_Item(1).Replace('USER:','').Trim()**
**$params=$list.get_Item(2).Replace('PARAMS:','').Trim()**
**Write-Output "$script $user $params"**
**if(Test-Path -Path "$OUTPUTDIR\$sensor\$script"){**
**$INST_OUTPUT="$OUTPUTDIR\$sensor\out$index"**
**New-Item -Path $INST_OUTPUT -type directory -ErrorAction SilentlyContinue -ErrorVariable err| Out-Null**
**if($err){**
**return**
**}**
**if($user.Length -eq 0){**
**## run script for session user**
**runCmd "instanceScript" "$OUTPUTDIR\$sensor\$script $INST_OUTPUT $PARAMS"**
**if($EC -eq 1){**
**## error during script execution**
**Get-Content $MAIN | Out-File -FilePath $ERRORFILEPATH -Append**
**}**
**}**
**else{**
**Write-Output "Windows ASD discovery does not support non-admin users"**
**}**
**}**
**else{**
**logError "checkFileExists" "There is no regular script file named $OUTPUTDIR\$sensor\$script"**
**Get-Content $MAIN | Out-File -FilePath $ERRORFILEPATH -Append**
**}**
**$list.RemoveRange(0,3)**
**}**
**##end while**
}
runSensorScript com.ibm.cdb.discover.sensor.app.mq.mqserver_7.6.0 sensorCommon.ps1
runSensorScript com.ibm.cdb.discover.sensor.asd.asdsensor_1.2.0 sensorCommon.ps1
runSensorScript com.ibm.cdb.discover.sensor.app.exchange.exchange_1.1.0 sensorCommon.ps1
runSensorScript com.ibm.cdb.discover.sensor.sys.windowscomputersystem_7.3.0 sensorCommon.ps1
runSensorScript com.ibm.cdb.discover.sensor.app.genericserver_7.6.0 sensorCommon.ps1
runSensorScript com.ibm.cdb.discover.sensor.app.j2ee.jboss7_1.0.0 sensorCommon.ps1
runSensorScript com.ibm.cdb.discover.sensor.app.web.iisserver_1.0.0 sensorCommon.ps1
########################## Create archive file #####################
if(Test-Path $OUTPUTDIR){
$zipDirectory = Get-Item $OUTPUTDIR
$zipfilename="$OUTPUTDIR.zip"
set-content $zipfilename (“PK” + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$zipPackage.CopyHere($zipDirectory.FullName)
#copyHere works asynchronously, short sleep is required
Start-Sleep -Seconds 4
}
########################## ASD statistics ##########################
if((Test-Path $ALLERRORS)-and(Get-Item $ALLERRORS).length -gt 0){
Write-Output “Execution finished with errors, check $ALLERRORS”
}
else{
Write-Output "Execution completed successfully."
Write-Output “Output file created: $zipfilename”
}
########################## Remove temporary files #####################
if(Test-Path -Path $MAIN){
Remove-Item -Path $MAIN
}
if(Test-Path -Path $OUTPUTDIR){
Remove-Item -Path $OUTPUTDIR -Recurse
}