question

Filip-2434 avatar image
0 Votes"
Filip-2434 asked IanXue-MSFT answered

Powershell add-type update my first script

Hello everybody.

I tried this:

 $code = @"
 using System;
 namespace HelloWorld
 {
  public class Program
  {
  public static void Main(){
  Console.WriteLine("Hello world!");
  }
  }
 }
 "@
     
 Add-Type -TypeDefinition $code -Language CSharp
 iex "[HelloWorld.Program]::Main()"

It work.
But when I change the "$code" variable script and "add-type" it secend time it show me an error.

109499-a.png

Why i can't update secend time my script?
How can i do it?

Thanks for help.


windows-server-powershell
a.png (18.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
1 Vote"
MotoX80 answered

Using the last example to generate a random class name looks to be the simplest solution.

https://stackoverflow.com/questions/25730978/powershell-add-type-cannot-add-type-already-exist

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
1 Vote"
IanXue-MSFT answered

Hi,

You have to use another type name because it must be unique within a session and can neither be updated nor removed.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.