# Script to Modify AWS Workspace Using Powershell

**URL:** <https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363>\
**Category:** PowerShell Help\
**Created:** [January 13, 2023, 3:43pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363 "2023-01-13T15:43:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PWSHNewbie](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@PWSHNewbie](https://forums.powershell.org/u/PWSHNewbie)\
**Post date:** [January 13, 2023, 3:43pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/1 "2023-01-13T15:43:29Z")

</div>

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:

````auto
# 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)

}#>```
````

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 13, 2023, 3:54pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/2 "2023-01-13T15:54:14Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 13, 2023, 4:10pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/3 "2023-01-13T16:10:20Z")

</div>

> [@PWSHNewbie](#):
>
> ```auto
> #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      
> })
> 
> ```

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 …

> [@PWSHNewbie](#):
>
> Cannot convert ‘System.Object’ to the type

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

---

<div class="post-metadata">

**Author:** ![PWSHNewbie](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@PWSHNewbie](https://forums.powershell.org/u/PWSHNewbie)\
**Post date:** [January 13, 2023, 4:24pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/4 "2023-01-13T16:24:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 13, 2023, 4:29pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/5 "2023-01-13T16:29:59Z")

</div>

```auto
Set-ExecutionPolicy RemoteSigned

```

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

```auto
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.

> **[about Requires - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-7.3)**
>
> Prevents a script from running without the required elements.

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

```

OK

```auto
$WorkspaceID = @()

```

Unnecessary …

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

```

OK

```auto
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`.

> **[Write-Host Considered Harmful](https://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/)**
>
> When you are writing or reviewing PowerShell scripts, I’d like you to remember the following rule of thumb: Using Write-Host is almost always wrong.

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.

```auto
$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 😉

Either use …

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

```

… or …

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

```

---

<div class="post-metadata">

**Author:** ![PWSHNewbie](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@PWSHNewbie](https://forums.powershell.org/u/PWSHNewbie)\
**Post date:** [January 13, 2023, 6:59pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/6 "2023-01-13T18:59:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/script-to-modify-aws-workspace-using-powershell/21363/7 "2024-05-16T20:16:04Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


