Safe use of Update-FormatData?
Posted
by
Steve B
on Super User
See other posts from Super User
or by Steve B
Published on 2012-11-28T10:51:06Z
Indexed on
2012/11/28
11:06 UTC
Read the original article
Hit count: 226
powershell
In a custom PowerShell module, I have at the top of my module definition this code:
Update-FormatData -AppendPath (Join-Path $psscriptroot "*.ps1xml")
This is working fine as all .ps1xml
files are loaded.
However, the module is sometimes loaded using Import-Module MyModule -Force
(actually, this is in the install script of the module).
In this case, the call to Update-FormatData
fails with this error :
Update-FormatData : There were errors in loading the format data file:
Microsoft.PowerShell, c:\pathto\myfile.Types.ext.ps1xml : File skipped because it was already present from "Microsoft.PowerShell".
At line:1 char:18
+ Update-FormatData <<<< -AppendPath "c:\pathto\myfile.Types.ext.ps1xml"
+ CategoryInfo : InvalidOperation: (:) [Update-FormatData], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Commands.UpdateFormatDataCommand
Is there a way to safely call this command?
I know I can call Update-FormatData
with no parameters, and it will update any known .ps1xml
file, but this would work only if the file has already been loaded.
Can I list somewhere the loaded format data files?
Here is a bit of background:
I'm building a custom module that is installed using a script. The install script looks like :
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
param()
process {
$target = Join-Path $PSHOME "Modules\MyModule"
if ($pscmdlet.ShouldProcess("$target","Deploying MyModule module"))
{
if(!(Test-Path $target)) { new-Item -ItemType Directory -Path $target | Out-Null }
get-ChildItem -Path (Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path) | copy-Item -Destination $target -Force
Write-Host -ForegroundColorWhite @"
The module has been installed. You can import it using :
Import-Module MyModule
Or you can add it in your profile ($profile)
"@
Write-Warning "To refresh any open PowerShell session, you should run ""Import-Module MyModule -Force"" to reload the module"
Import-Module MyModule -Force
Write-Warning "This session has been refreshed."
}
}
MyModule defines, as first statement, this line :
Update-FormatData -AppendPath (Join-Path $psscriptroot "*.ps1xml")
As I updated my $profile
to always load this module, the Update-Path
command has been called when I run the install script. In the install script, I force import the module, which be fire again the module, and then, the Update-Path
call
© Super User or respective owner