Error while execution powershell code on System Center Virtual Machine Manager - powershell

I'm facing an issue of getting all vms configuration ( using Get-ScVirtualMachine command) into an array from an input file.
The code is this one below
$VmsList = Get-Content C:\VmsList.txt
foreach($vm in $VmsList){
$Result += Get-SCVirtualMachine -Name $vm
}
And I have this error
Method invocation failed because [Microsoft.SystemCenter.VirtualMachineManager.VM] does not contain a method named 'op_Addition'.
At line:3 char:1
$Result += Get-SCVirtualMachine -Name $vm
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [Microsoft.SystemCenter.VirtualMachineManager.VM] does not contain a method named 'op_Addition'.
At line:3 char:1
$Result += Get-SCVirtualMachine -Name $vm
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Get-SCVirtualMachine : Cannot validate argument on parameter 'Name'. The character length (0) of the argument is too short. Specify an argument with a length that
is greater than or equal to "1", and then try the command again.
At line:3 char:39
$Result += Get-SCVirtualMachine -Name $vm
~~~
CategoryInfo : InvalidData: (:) [Get-SCVirtualMachine], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.GetVMCmdlet

I forgot to declare the array $result = #()
I resolved the issue.

Related

How to write to worksheet.range with system.collections.ArrayList by Powershell?

when I try to write to worksheet.range with system.collections.ArrayList, It was failed, but because it was an One-dimensional array when I transfer it to be system.Array, it was been an One-dimensional array, How can I transfer it into a two-dimensional array? or how to write to worksheet?
Error:
[DBG]: PS C:\Users\xxxxxxxxx>>
Missing parameter does not have a default value.
Parameter name: parameters
At C:\Users\xxxxxxxxx\Desktop\txt2excel.ps1:148 char:1
$ws.Range($startCell,$endCell).Value2 = $target_columns
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
[DBG]: PS C:\Users\xxxxxxxxx>> $ws.Range($startCell,$endCell).Value2 = [System.Array]$target_columns
Exception from HRESULT: 0x800A03EC
At line:1 char:1
$ws.Range($startCell,$endCell).Value2 = [System.Array]$target_columns
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
main code:
$original_file = Get-Content $originalTXT[1]
$target_columns = New-Object System.Collections.ArrayList
foreach ($ss in $original_file) {
[System.Collections.ArrayList]$target_row_temp = $ss.Split("|")
$string_temp = $target_row_temp.GetRange(0,2)
$target_columns.Add($string_temp) | Out-Null
$target_row_temp.RemoveRange(0,2)
$target_range.Add($target_row_temp) | Out-Null
}
$startCell = ws.Cells.Item(11,2)
$endCell = $ws.Cells.Item(10 + $excelHigh,3)
$ws.Range($startCell,$endCell).Value2 = [System.Array]$target_columns

How to call a powershell script to another powershell script with arguments?

I just need to call another script and pass the parameters to it.
I tried doing invoke-expression to access it, i tried using &, and nothing worked
I tried doing the following:
$hostfile = "C:\Users\username\Desktop\csvfile_test.csv"
$outFile = ".\testerFile.xlsx"
& '.\organizer.ps1' "-csvFile $hostfile -outputPath $outFile "
Invoke-Expression 'C:\Users\username\Desktop\organizer.ps1' "$hostfile $outFile"
I receive the following errors:.
with ampersand (&):
PS C:\Users\username\Desktop> C:\Users\username\Desktop\scanner.ps1
Exception calling "ReadLines" with "1" argument(s): "The given path's format is not supported."
At C:\Users\username\Desktop\scanner.ps1:48 char:1
+ [System.IO.File]::ReadLines("$csvFile") | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
with invoke-expression:
Exception calling "ReadLines" with "1" argument(s): "The given path's format is not supported."
At C:\Users\username\Desktop\scanner.ps1:48 char:1
+ [System.IO.File]::ReadLines("$csvFile") | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
Invoke-Expression : A positional parameter cannot be found that accepts argument 'C:\Users\username\Desktop\csvfile_test.csv .\testerFile.xlsx'.
At C:\Users\username\Desktop\scanner.ps1:69 char:1
+ Invoke-Expression 'C:\Users\username\Desktop\organizer.ps1' "$host ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand
When you write this:
& '.\organizer.ps1' "-csvFile $hostfile -outputPath $outFile "
you are passing a single parameter to the script (one quoted string). That's not what you want.
This is what you need:
$hostfile = "C:\Users\username\Desktop\csvfile_test.csv"
$outFile = ".\testerFile.xlsx"
.\organizer.ps1 -csvFile $hostfile -outputPath $outFile
First, you don't need the & (invocation) operator because your command name (the organizer.ps1 script in the current location, in this example) doesn't contain spaces. (You can add it if you want, but it's unnecessary in this scenario.)
Second, the -csvFile and -outputPath parameters each require a string.

How to check if VMHost is configured with Syslog forwarding

I want to check if VMHost is configured with Syslog forwarding
I have written the following cmdlets but it is not giving "Syslog forwarding" status.
$esxcli = Get-EsxCli -VMHost abc.com
$esxcli.system.syslog | Select-Object *
$esxcli.system.syslog | gm is returning an error
gm : You must specify an object for the Get-Member cmdlet.
At line:1 char:25
+ $esxcli.system.syslog | gm
+ ~~
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand
$esxcli.system.syslog.config.get() is returning an error:
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $esxcli.system.syslog.config.get()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PowerCLI includes the Get-VMHostSysLogServer cmdlet. In the context of your question, this command would return the Syslog Server for the VMHost abc.com:
Get-VMHostSysLogServer -VMHost abc.com

Object reference not set to an instance of an object. Error when running command Get-AzureRmRecoveryServicesAsrRecoveryPlan

Object reference not set to an instance of an object.
Error when running command Get-AzureRmRecoveryServicesAsrRecoveryPlan...
PS C:\WINDOWS\system32> $rpname = "myrecoveryplan"
$recoplan = Get-AzureRmRecoveryServicesAsrRecoveryPlan -Name $rpname
Get-AzureRmRecoveryServicesAsrRecoveryPlan : Operation failed.
Object reference not set to an instance of an object.
At line:2 char:13
+ $recoplan = Get-AzureRmRecoveryServicesAsrRecoveryPlan -Name $rpname
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureRmReco...AsrRecoveryPlan], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.RecoveryServices.SiteRecovery.GetAzureRmRecoveryS
ervicesAsrRecoveryPlan
I have updated My powershell and it seems moving on. But I got another blocker. $Context= Set-AzureRmRecoveryServicesAsrVaultContext -Vault $Vault
$recoplan = Get-AzureRmRecoveryServicesAsrRecoveryPlan -Name $rpname
Start-AzureRmSiteRecoveryTestFailoverJob -RecoveryPlan $recoplan -Direction "PrimaryToRecovery" -VMNetwork "asrtestfailtarget"
Start-AzureRmSiteRecoveryTestFailoverJob : Cannot bind parameter 'RecoveryPlan'. Cannot convert the
"Microsoft.Azure.Commands.RecoveryServices.SiteRecovery.ASRRecoveryPlan" value of type
"Microsoft.Azure.Commands.RecoveryServices.SiteRecovery.ASRRecoveryPlan" to type
"Microsoft.Azure.Commands.SiteRecovery.ASRRecoveryPlan".
At line:5 char:56
+ ... rt-AzureRmSiteRecoveryTestFailoverJob -RecoveryPlan $recoplan -Direct ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-AzureRmSiteRecoveryTestFailoverJob], Parameter
BindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.SiteRecovery.Start
AzureRmSiteRecoveryTestFailoverJob

Showing error while adding element in List[int] in powershell?

$sumList= New-Object System.Collections.Generic.List[int]
$eventList = New-Object System.Collections.Generic.List[string]
$preSumList= New-Object System.Collections.Generic.List[int]
$threadInitDelay = [int]$commaSplit[++$eventIndex].Split(':')[1].Trim()
$preProcessTime = [int]$commaSplit[++$eventIndex].Split(':')[1].Trim()+[int]$commaSplit[++$eventIndex].Split(':')[1].Trim()+$commaSplit[[int]++$eventIndex].Split(':')[1].Trim()
$respTime = [int]$commaSplit[++$eventIndex].Split(':')[1].Trim();
$index =$eventList.FindIndex({param([string]$s) return $s -like 'hi'})
if($index -eq -1){
$eventList.Add($eventName)
$sumList.Add($threadInitDelay)
$respSumList.Add($respTime)
Getting this error:
Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:\Powercel\PowershellPractice.ps1:151 char:19
+ $sumList.Add <<<< ($threadInitDelay)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:\Powercel\PowershellPractice.ps1:152 char:23
+ $respSumList.Add <<<< ($respTime)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:\Powercel\PowershellPractice.ps1:153 char:23
+ $respMaxTime.Add <<<< ($respTime)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.Int32[]] doesn't contain a method named 'Add'.
At D:\Powercel\PowershellPractice.ps1:155 char:19
+ $minTime.Add <<<< ($threadInitDelay)
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Could you please help me out?
A possible scenario is that you were testing and actually casted those variables like the error states. The error is not wrong. Those variables are not the list types that you have in your code. I can almost reproduce this issue:
PS M:\Scripts\Inno\Output> [int[]]$sumlist = 5,5
PS M:\Scripts\Inno\Output> $sumlist.GetType().Fullname
System.Int32[]
PS M:\Scripts\Inno\Output> $sumlist.Add(5)
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $sumlist.Add(5)
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
PS M:\Scripts\Inno\Output> $sumlist = New-Object System.Collections.Generic.List[int]
While I didn't get the same error as you the point is the type of the variable didn't change even though I tried to recasted it. That is because of property of the variable explained in this answer by PetSerAl. Have a look at the command Get-Variable sumlist | ft Name,Attributes and you should see System.Management.Automation.ArgumentTypeConverterAttribute. Removing the variable and starting again I get your expected behavior.
PS M:\Scripts\Inno\Output> Remove-Variable sumlist
PS M:\Scripts\Inno\Output> $sumlist = New-Object System.Collections.Generic.List[int]
PS M:\Scripts\Inno\Output> $sumlist.Add(5)
PS M:\Scripts\Inno\Output> $sumlist.GetType().FullName
System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]