# powershell script question

**URL:** <https://forums.powershell.org/t/powershell-script-question/2023>\
**Category:** PowerShell Help\
**Created:** [October 31, 2013, 8:41pm UTC](https://forums.powershell.org/t/powershell-script-question/2023 "2013-10-31T20:41:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![1richierich1](https://avatars.discourse-cdn.com/v4/letter/1/b2d939/32.png) [@1richierich1](https://forums.powershell.org/u/1richierich1)\
**Post date:** [October 31, 2013, 8:41pm UTC](https://forums.powershell.org/t/powershell-script-question/2023/1 "2013-10-31T20:41:11Z")

</div>

Hi guys  
How do i run ps script in multiple domains  
Example  
$servers = Gc c:\servers.txt  
$cred1 = get-credential

Foreach($server in $servers {  
Try { gwmi win32\_timezone }  
Try { gwmi win32\_timezone -credential $cred1}  
Catch{ write-host “whatever”  
}

I get error saying try needs a catch block

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 31, 2013, 10:11pm UTC](https://forums.powershell.org/t/powershell-script-question/2023/2 "2013-10-31T22:11:51Z")

</div>

Each try statement needs its own catch block; you can’t chain them together like that. You can, however, put multiple statements into a single Try block. If the first statement succeeds without producing a terminating error, the next one is executed, and so on (though that logic doesn’t seem quite right for what you’re doing; I would assume that you want to execute the second call to Get-WmiObject only if the first one failed. For that, you can put the second call into the first one’s catch block.)

On a side note, you’re not actually using the $server variable in the calls to Get-WmiObject in that code, so they would all be executing locally. You’ll also want to add “-ErrorAction Stop” to each of the calls to Get-WmiObject; this ensures that if they do produce any errors, they are treated as Terminating errors (which is the only thing a try/catch block will see, in PowerShell.)

Try this:

```
foreach ($server in $servers) {
    try {
        gwmi win32_timezone -ComputerName $server -ErrorAction Stop
    } catch {
        try {
            gwmi win32_timezone -credential $cred1 -ComputerName $server -ErrorAction Stop
        } catch {
            Write-Host "Whatever"
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![1richierich1](https://avatars.discourse-cdn.com/v4/letter/1/b2d939/32.png) [@1richierich1](https://forums.powershell.org/u/1richierich1)\
**Post date:** [November 14, 2013, 4:12am UTC](https://forums.powershell.org/t/powershell-script-question/2023/3 "2013-11-14T04:12:34Z")

</div>

after reading about 15-20 times and troubleshooting, i understood this… thanks a lot Dave

---

<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:56pm UTC](https://forums.powershell.org/t/powershell-script-question/2023/4 "2024-05-16T20:56:51Z")

</div>

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


