# Add-type compile errors when running script as SYSTEM

**URL:** <https://forums.powershell.org/t/add-type-compile-errors-when-running-script-as-system/12632>\
**Category:** PowerShell Help\
**Created:** [May 28, 2019, 9:10pm UTC](https://forums.powershell.org/t/add-type-compile-errors-when-running-script-as-system/12632 "2019-05-28T21:10:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebredmond](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ebredmond](https://forums.powershell.org/u/ebredmond)\
**Post date:** [May 28, 2019, 9:10pm UTC](https://forums.powershell.org/t/add-type-compile-errors-when-running-script-as-system/12632/1 "2019-05-28T21:10:37Z")

</div>

When I attempt to run the following snippet of code as SYSTEM

```
add-type "PSusing System.Net;PSusing System.Security.Cryptography.X509Certificates;public class TrustAllCertsPolicy : ICertificatePolicy {public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest request, int certificateProblem) {return true;}}";$AllProtocols = [System.Net.SecurityProtocolType]"Ssl3,Tls,Tls11,Tls12";[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols;[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
```

I get the following errors:

```
add-type : c:\Windows\Temp\1xdyb5zc\1xdyb5zc.0.cs(1) : A namespace cannot directly contain members
such as fields or methods
c:\Windows\Temp\1xdyb5zc\1xdyb5zc.0.cs(1) : >>> PSusing System.Net;PSusing
System.Security.Cryptography.X509Certificates;public class TrustAllCertsPolicy : ICertificatePolicy
{public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest
request, int certificateProblem) {return true;}}
At line:1 char:1
+ add-type "PSusing System.Net;PSusing System.Security.Cryptography.X509Certificat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (c:\Windows\Temp...elds or methods:CompilerError) [Add-T
ype], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

add-type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ add-type "PSusing System.Net;PSusing System.Security.Cryptography.X509Certificat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

New-Object : Cannot find type [TrustAllCertsPolicy]: verify that the assembly containing this type
is loaded.
At line:1 char:478
+ ... ficatePolicy = New-Object TrustAllCertsPolicy
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
```

Is there something about running this chunk of code as SYSTEM which is not allowing it to run. It runs properly when running as a normal user.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 28, 2019, 9:37pm UTC](https://forums.powershell.org/t/add-type-compile-errors-when-running-script-as-system/12632/2 "2019-05-28T21:37:09Z")

</div>

What is PSusing ?

You can read the examples from below documentation for Add-Type cmdlet.

> **[Add-Type (Microsoft.PowerShell.Utility) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-5.1)**
>
> The Add-Type cmdlet lets you define a Microsoft .NET Framework class in your PowerShell session. You can then instantiate objects, by using the New-Object cmdlet, and use the objects just as you would use any .NET Framework object. If you add an...

---

<div class="post-metadata">

**Author:** ![ebredmond](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@ebredmond](https://forums.powershell.org/u/ebredmond)\
**Post date:** [May 29, 2019, 8:12am UTC](https://forums.powershell.org/t/add-type-compile-errors-when-running-script-as-system/12632/3 "2019-05-29T08:12:00Z")

</div>

PSUsing was something I saw I needed to change to because I was getting errors using well using.

I made the change and it seems to have worked. Thank you.

---

<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:32pm UTC](https://forums.powershell.org/t/add-type-compile-errors-when-running-script-as-system/12632/4 "2024-05-16T20:32:43Z")

</div>

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


