Script to Modify AWS Workspace Using Powershell

Hello,

I was trying to work on something that I felt would not be a timely or overly complex endeavor. Unfortunately I ran into the thrown error “Edit-WKSWorkspaceProperty : Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter ‘WorkspaceId’. Specified method is not supported.”

While trying to understand the why before getting into possible “how’s” on correcting it, I’ve confused myself more so. I at least feel that the values in the array or how I have assigned said values from the CSV is where my problem lies compared to the parameter’s value type that’s expected.

The full code is as follows:

# Powershell script execution policy specification to allow running of this script
# Latest version of Windows Server shoould be defaulted to this policy already
Set-ExecutionPolicy RemoteSigned


# Importing Cmdlets from the Powershell modules ActiveDirectory & AWSPowershell
Import-Module ActiveDirectory

Import-Module AWSPowershell


Initialize-AWSDefaultConfiguration -ProfileName Company -Region us-west-2

#Array for holding workspace IDs from CSV file
$WorkspaceID = @()


$WorkspaceRegion = "us-west-2"


$WorkspaceProtocol = "WSP"


Import-Csv c:\Temp\WorkspaceIDs.csv | Foreach-object {
	Write-Host "Workspace ID to migrate to WSP protocol on us-west-2 is as follows...."
	Write-Host "$($_.ID)"`n`n
}

#Import contents and store it in the array named $WorkspaceID	
$WorkspaceID = Import-Csv c:\Temp\WorkspaceIDs.csv


#Trying a Foreach() method to process actions over each iteration
#Error being thrown: "Edit-WKSWorkspaceProperty : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'WorkspaceId'. Specified method is not supported."
$WorkspaceID.ForEach({
        Edit-WKSWorkspaceProperty -WorkspaceID $WorkspaceID -Region $WorkspaceRegion -WorkspaceProperties_Protocol $WorkspaceProtocol      
})


#Not primary objective, but possibly would like to verify each ID's properties after the edit protocol change is successful.
#Will most likely throw the same error "Edit-WKSWorkspaceProperty : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'WorkspaceId'. Specified method is not supported."
#Using comment block, not ready to test segment.
<#foreach ($ID in $WorkspaceID)
{
    Get-WKSWorkspace -Region $WorkspaceRegion -WorkspaceId $WorkspaceID | Select-Object -ExpandProperty WorkspaceProperties)

}#>```

Please go back, edit your question and fix the formatting of the code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

How does your CSV WorkspaceIDs.csv look like? You may share a few lines of it (sanitized from sensitive information of course)

Is there a particular reason why you’re using the dotNet Foreach() method instead of either a Foreach-Object or a foreach loop?

An error message complaining about …

… usually indicates that you used an array where you should use a single element of the expected type.

My CSV file is just a single column with a header called ID, then each cell beneath that has values that begin with ws- followed by various letters and a few numbers when the workspace ID is generated in the system. As for the error, would I simply not be able to automate that command or particularly the parameter against a list of IDs one at a time from a array of values?

I even tried starting it as Foreach ($ID in $WorkspaceID) { with the same command inside of the brackets

Set-ExecutionPolicy RemoteSigned

This is bullshit - you cannot change the executionpolicy for scripts from inside a script!!!

Import-Module ActiveDirectory
Import-Module AWSPowershell

Both unnecessary … correctly installed modules will be loaded automatically.
A better option would be to use the #requires statement. This would prevent the script from running if the necessary modules were not installed.

Initialize-AWSDefaultConfiguration -ProfileName Company -Region us-west-2

OK

$WorkspaceID = @()

Unnecessary …

$WorkspaceRegion = "us-west-2"
$WorkspaceProtocol = "WSP"

OK

Import-Csv c:\Temp\WorkspaceIDs.csv | 
Foreach-object {
	Write-Host "Workspace ID to migrate to WSP protocol on us-west-2 is as follows...."
	Write-Host "$($_.ID)"`n`n
}

Unnecessary … consider using proper logging if needed instead of Write-Host.

If you insist of outputting unnecessary pixels on the console you should import your CSV file once, save it to a variable and use it as often as needed.

$WorkspaceID = Import-Csv c:\Temp\WorkspaceIDs.csv

Since it probably is more than one element you may use a variable name indicating an array or a list … $WorkspaceIDList maybe :wink:

Either use …

foreach($WorkspaceID in $WorkspaceIDList.ID){
        Edit-WKSWorkspaceProperty -WorkspaceID $WorkspaceID -Region $WorkspaceRegion -WorkspaceProperties_Protocol $WorkspaceProtocol      
}

… or …

foreach($WorkspaceID in $WorkspaceIDList){
    Edit-WKSWorkspaceProperty -WorkspaceID $WorkspaceID.ID -Region $WorkspaceRegion -WorkspaceProperties_Protocol $WorkspaceProtocol      
}

Thank you. I need to review my script this further as now no errors are thrown, but the task intended via the command is never occurring.