I made some code that uses the New-Object command from the command line Powershell, but this error popped up whenever I tried to load anything from System.Windows:
PS C:\Users\USER> New-Object System.Windows.FontWeight
New-Object : Cannot find type [System.Windows.FontWeight]: verify that the assembly containing this type is loaded.
At line:1 char:1
+ New-Object System.Windows.FontWeight
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
This code works fine in Powershell ISE:
PS C:\Users\USER> New-Object System.Windows.FontWeight
Normal
If anyone can tell me what the problem is and how to fix it, that would be greatly appreciated!
The problem is, that the assembly PresentationCore that contains type System.Windows.FontWeight is not loaded in your PowerShell session. Seems like PowerShell ISE and PowerShell Console do not preload the same assemblies. You can load the corresponding assembly as follows:
Add-Type -AssemblyName PresentationCore
PowerShell ISE loads the containing assembly on startup to build its own GUI.
In powershell.exe you'll have to load it yourself:
Add-Type -AssemblyName PresentationCore |Out-Null
The FontWeight Structure is present in the System.Windows namespace and PresentationCore assembly. As evident from the error, you need to add the assembly PresentationCore to your PowerShell command line. Use this and you will be good to go.
Add-Type -Assembly PresentationCore
Related
I have the current version of 64 bit Windows 10 installed.
I can open a Windows PowerShell window and enter the following command to execute my PowerShell script. The script execute without error.
PS C:\Users\david\Desktop\test> ./messagebox.ps1
I want to execute the same script from a Windows Command Prompt window. When I enter the follow command, I get the displayed error messages.
C:\Users\david\Desktop\test>powershell -ExecutionPolicy Bypass -file messagebox.ps1
At C:\Users\david\Desktop\test\messagebox.ps1:81 char:14
+ Class Form : System.Windows.Forms.Form
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Form].
At C:\Users\david\Desktop\test\messagebox.ps1:102 char:21
+ return [System.Windows.Forms.MessageBox]::Show($messsage, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
At C:\Users\david\Desktop\test\messagebox.ps1:108 char:21
+ return [System.Windows.Forms.MessageBox]::Show($messsage, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
The script includes the following lines which I thought would include the correct assembly.
$n = new-object System.Reflection.AssemblyName("System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[System.AppDomain]::CurrentDomain.Load($n) | Out-Null
You did not post enough code to actually reproduce the issue, but this works for me:
Add-Type -AssemblyName System.Windows.Forms | Out-Null
[System.Windows.Forms.MessageBox]::Show("Hello World")
I assume you can extend this to whatever version of Show() you need.
See also PowerShell Magazine
Why doesn't Add-Type work with the BizTalk 2010 Microsoft.BizTalk.ExplorerOM assembly?
I can happily work the ExplorerOM objects if I load the old way via reflection ...
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
But I use the proper way with Add-Type ...
Add-Type -AssemblyName Microsoft.BizTalk.ExplorerOM
I get the cannot add type error ...
Add-Type : Cannot add type. The assembly 'Microsoft.BizTalk.ExplorerOM' could not be found.
At E:\loadexplorerom.ps1:5 char:1
+ Add-Type -AssemblyName Microsoft.BizTalk.ExplorerOM
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.BizTalk.ExplorerOM:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
I've checked my GAC and the BizTalk 2010 ExplorerOM is in there ...
Microsoft.BizTalk.ExplorerOM, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL
PowerShell is very much limited in loading predefined set of assemblies with their partial name.
In your case also it is the same. You have to tell powershell to load that particular assembly for the Biztalk server.
In other words, you have to give the full path of the dll :
Sample:
Add-Type -Path
C:\Windows\Microsoft.NET\assembly\GAC_64\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll
Try adding this also:
Add-Type -AssemblyName ('Microsoft.BizTalk.ExplorerOM, Version=3.0.1.0, ' + 'Culture=neutral, PublicKeyToken=31bf3856ad364e35' + 'processorArchitecture=MSIL')
I need to zip a folder using powershell from within SQLPS runtime. I tried the following from PS SQLSERVER:\> prompt:
$dest = "C:\SomePath\FolderToBeZipped"
$zipfile = $dest + ".zip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($dest,$zipfile)
When run, I get the below error :
Add-Type : Cannot add type. The assembly 'system.io.compression.filesystem' could not be found.
At C:\SomePath\Compress.ps1:10 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : ObjectNotFound: (system.io.compression.filesystem:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeComm
and
Add-Type : Cannot add type. One or more required assemblies are missing.
At C:\SomePath\Compress.ps1:10 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
But if I exit out of PS SQLSERVER:\> runtime and execute from normal PS command prompt it is able to compress successfully. Please help how to compress from SQLSERVER powershell runtime. I even tried to invoke a separate .ps1 script (for compressing) using Invoke-Expression from within PS SQLSERVER:\> runtime, but failed. Please help
I'm sending a zip and the powershell script to computers in our organisation through our management software and then executing the powershell script through command prompt. The script unzips the folder, installs a program and adds some registry keys to the registry.
If the machine doesnt say that a policy is preventing execution of the script, it runs as expected, the program is installed and the registry keys are imported. But if it does give the warning and I set the policy to unrestricted I get the below error error. I am on Windows 8.1 and using Powershell 4.0
Id not be found.
At C:\Temp\tps1.ps1:5 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : ObjectNotFound: (system.io.compression.filesyste
m:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands
.AddTypeCommand
Add-Type : Cannot add type. One or more required assemblies are missing.
At C:\Temp\tps1.ps1:5 char:9
+ Add-Type <<<< -assembly "system.io.compression.filesystem"
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationExc
eption
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Comman
ds.AddTypeCommand
Unable to find type [io.compression.zipfile]: make sure that the assembly conta ining this type is loaded.
At C:\Temp\tps1.ps1:7 char:25
+ [io.compression.zipfile] <<<< ::ExtractToDirectory($BackUpPath, $destination) + CategoryInfo : InvalidOperation: (io.compression.zipfile:String
) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
anyone have any ideas?
on systems where .NET 4.5 is not installed, use:
$BackUpPath = "C:\Temp\Install.zip"
$Destination = "C:\Temp"
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($BackUpPath )
$destination = $shell_app.namespace($Destination )
$destination.Copyhere($zip_file.items(),0x4)
on systems with.NET 4.5 use:
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::ExtractToDirectory($BackUpPath, $destination)
A script on technet combines the above 2 methods.
On PowerShell V5 , this is much simplified:
Expand-Archive $BackUpPath -destination $Destination
there are other ways to unzip files using 3rd party libraries such as 7-zip, etc but that creates a dependency.
Note: Powershell execution policies can be set using group policy. see link here
I am trying to write PowerShell and am failing miserably.
Set-ExecutionPolicy Unrestricted
Import-Module -Assembly PowerShellXrm.Framework.CI.PowerShell.dll
and
Set-ExecutionPolicy Unrestricted
Import-Module -Assembly "PowerShellXrm.Framework.CI.PowerShell.dll"
and get the following error.
Import-Module : Cannot bind parameter 'Assembly'. Cannot convert the
"PowerShellXrm.Framework.CI.PowerShell.dll" value of type "System.String"
to type "System.Reflection.Assembly".
At line:1 char:25
+ Import-Module -Assembly PowerShellXrm.Framework.CI.PowerShell.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Import-Module], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ImportModuleCommand
The PowerShell script is saved in the same location as the PowerShellXrm.Framework.CI.PowerShell.dll assembly. I have also tried including the full path to the assembly with no luck.
If you want to import a PowerShell module from a DLL file simply pass the filename:
Import-Module 'PowerShellXrm.Framework.CI.PowerShell.dll'
Use the full path if the file is not located in one of the folders listed in $env:PSModulePath:
Import-Module 'C:\path\to\PowerShellXrm.Framework.CI.PowerShell.dll'
As documented the -Assembly parameter is for importing assembly objects, not assembly files.
-Assembly<Assembly[]>
Imports the cmdlets and providers implemented in the specified assembly objects. Enter a variable that contains assembly objects or a command that creates assembly objects. You can also pipe an assembly object to Import-Module.
If you want to use -Assembly parameter you can use the following:
$assembly = [System.Reflection.Assembly]::LoadFrom('PowerShellXrm.Framework.CI.PowerShell.dll')
Import-Module -Assembly $assembly