• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

V2 CTP3: Is there a way to undo the action of Add-Type?

C

Chuck Heatherly

#1
I have this PowerShell script, Abc.ps1:

$source = @"
public class Abc
{
public void One()
{
System.Console.WriteLine("Abc");
}
}
"@

Add-Type -TypeDefinition $source

When I run this from a PowerShell prompt, it's fine. If I edit the
script and modify it such that the compiled code is different (say
change the "Abc" string to "Def"), then when I try to run the script
in the same prompt window, I get this error:

Add-Type : Cannot add type. The type name 'Abc' already exists.
At C:\Public\Abc.ps1:11 char:9
+ Add-Type <<<< -TypeDefinition $source
+ CategoryInfo : InvalidOperation: (Abc:String)
[Add-Type], Exception
+ FullyQualifiedErrorId :
TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand

I don't want to have to keep opening a new prompt window everytime I
change the source, so is there a way I can remove this type, or
otherwise keep this error from happening?

Thanks,
Chuck
 

My Computer

A

Alex K. Angelopoulos

#2
The problem you're encountering is that once you load an assembly into a
..NET application, the types it contains are not released until the
application shuts down. There are two workarounds that can allow you to do
something that looks like this, but nothing that is truly equivalent to
erasing the type and starting over.

One technique is to modify the name each time you try to use it - awkward
and not pretty. The other technique is to run the code within a new
pssession each time you change it. I haven't tried this yet since I don't
have WinRM configured properly for local loopback, but I believe it should
create an isolated local pssession that you can try things with and then
release when un-needed.


"Chuck Heatherly" <chuck.heatherly@xxxxxx> wrote in message
news:k7pgv4h4t6a7hjqaue2k13g1sdbe2f3bnq@xxxxxx

> I have this PowerShell script, Abc.ps1:
>
> $source = @"
> public class Abc
> {
> public void One()
> {
> System.Console.WriteLine("Abc");
> }
> }
> "@
>
> Add-Type -TypeDefinition $source
>
> When I run this from a PowerShell prompt, it's fine. If I edit the
> script and modify it such that the compiled code is different (say
> change the "Abc" string to "Def"), then when I try to run the script
> in the same prompt window, I get this error:
>
> Add-Type : Cannot add type. The type name 'Abc' already exists.
> At C:\Public\Abc.ps1:11 char:9
> + Add-Type <<<< -TypeDefinition $source
> + CategoryInfo : InvalidOperation: (Abc:String)
> [Add-Type], Exception
> + FullyQualifiedErrorId :
> TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
>
> I don't want to have to keep opening a new prompt window everytime I
> change the source, so is there a way I can remove this type, or
> otherwise keep this error from happening?
>
> Thanks,
> Chuck
 

My Computer

J

Josh Einstein

#3
As Alex stated, no you can't do that but why are you using C# as an
interactive scripting environment within PowerShell? You could grab
something like Snippet Compiler or LINQPad to do that and it'll be far
better suited at experimenting with C# blocks than doing it with PowerShell
which is awkward at best.

I presume Add-Type is mostly intended to be an alternative to bundling
pre-compiled DLL's.

"Chuck Heatherly" <chuck.heatherly@xxxxxx> wrote in message
news:k7pgv4h4t6a7hjqaue2k13g1sdbe2f3bnq@xxxxxx

> I have this PowerShell script, Abc.ps1:
>
> $source = @"
> public class Abc
> {
> public void One()
> {
> System.Console.WriteLine("Abc");
> }
> }
> "@
>
> Add-Type -TypeDefinition $source
>
> When I run this from a PowerShell prompt, it's fine. If I edit the
> script and modify it such that the compiled code is different (say
> change the "Abc" string to "Def"), then when I try to run the script
> in the same prompt window, I get this error:
>
> Add-Type : Cannot add type. The type name 'Abc' already exists.
> At C:\Public\Abc.ps1:11 char:9
> + Add-Type <<<< -TypeDefinition $source
> + CategoryInfo : InvalidOperation: (Abc:String)
> [Add-Type], Exception
> + FullyQualifiedErrorId :
> TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
>
> I don't want to have to keep opening a new prompt window everytime I
> change the source, so is there a way I can remove this type, or
> otherwise keep this error from happening?
>
> Thanks,
> Chuck
 

My Computer