![ExportedCommand 空自定义模块 PowerShell](https://linux22.com/image/667422/ExportedCommand%20%E7%A9%BA%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9D%97%20PowerShell.png)
我在 Powershell 中有一个自定义模块。它由两个文件 Copy-Move.psm1 和 Copy-Move.psd1 组成。
在 Copy-Move.psm1 中我有几个函数,我仅通过在末尾使用以下命令导出函数“Copy-MoveFiles”
function Copy-MoveFiles
{..}
Export-ModuleMember Copy-MoveFiles
我已将文件放在以下目录中:
> C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Copy-Move
我已经启动了 Powershell 命令屏幕并执行了以下命令:
Import-Module Copy-Move
当我执行以下命令时,那里没有错误:
Get-Module Copy-Move
我得到以下结果:
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0 Copy-Move
如您所见,尽管我明确地将函数导出为模块成员,但我没有导出任何函数。模块清单不会过滤要导出的函数,因为我使用通配符导出了以下函数:
FunctionsToExport = '*'
我肯定忘记了什么。我已经这样做过几十次了,但这次似乎无法正常工作。
模块代码:
复制-移动-psm1
function Copy-MoveFiles
{
<#
.SYNOPSIS
Deze functie maakt het mogelijk bestanden te kopieren of te verplaatsen
door middel van een configuratiebestand.
.DESCRIPTION
Deze functie maakt het mogelijk om meerdere kopieer- of verplaatstaken uit
te voeren d.m.v. een enkel configuratiebestand.
.PARAMETER config
Pad naar het configuratiebestand
.PARAMETER logdir
Pad waar het logbestand gemaakt moet worden.
.EXAMPLE
Copy-MoveFiles -config "C:\config.csv" -logdir "C:\log"
#>
param
(
[Parameter(Mandatory=$true,Position=1)][string] $config = ""
, [Parameter(Mandatory=$false,Position=2)][string] $logdir = $PSScriptRoot + "\Log"
)
# Controleren of het pad naar het configuratiebestand klopt
if(Test-Path $config)
{
# Aanmaken van de timestamp
$timestamp = Get-Date -format yyyyMMdd
# Opzetten van het logfile
$logfile = $logdir + "\logfile_$timestamp.log"
# Laden van de verschillende acties
$actions = Import-Csv $config -Delimiter ';'
# Controleer of er acties aanwezig zijn
if($actions.count -ge 1)
{
Write-Logfile -logfile $logfile -level 3 -text "Actions found. Starting up..."
# Loop door alle acties heen
foreach($a in $actions)
{
# Ophalen van de
$source = $a.source
$destination = $a.destination
$createsubfolder = $a.createsubfolder
$action = $a.action
$filter = $a.filter
# Controleer of er een speciale subfolder aangemaakt moet worden
if($createsubfolder -ne ''){
switch($createsubfolder)
{
"[date]"{$destination = $destination + "\$timestamp"}
}
}
# Kijken of de doeldirectory bestaat
if(!(Test-Path($destination)))
{
# Maak de doeldirectory aan als deze niet bestaat
Write-Logfile -logfile $logfile -level 1 -text "Destination $destination doesn't exist. Creating.."
Show-Message -level 1 -text "Destination $destination doesn't exist. Creating.."
New-Item -ItemType directory -Path $destination | Out-Null
}
# Check to see if the source folder exists
if(Test-Path($source))
{
# Get the files from the source folder
$files = Get-ChildItem -Path $source | where { ! $_.PSIsContainer }
# Check if the source folder contains files
if($files.count -ge 1)
{
# Bekijk welke actie uitgevoerd moet worden
switch($action.ToUpper())
{
"COPY"
{
Write-Logfile -logfile $logfile -level 1 -text "Copying from $source to $destination."
Show-Message -level 1 -text "Copying from $source to $destination."
# Probeer het commando uit te voeren
try
{
# Bekijk of een filter benodigd is
if($filter -ne '')
{
ROBOCOPY $source $destination $filter | Out-Null
}
else
{
ROBOCOPY $source $destination | Out-Null
}
} catch
{
Write-Logfile -logfile $logfile -level 3 -text "Error executing ROBOCOPY command: $_"
Show-Message -level 3 -text "Error executing ROBOCOPY command: $_"
}
}
"MOVE"
{
Write-Logfile -logfile $logfile -level 1 -text "Moving from $source to $destination."
Show-Message -level 1 -text "Moving from $source to $destination."
# Probeer het commando uit te voeren
try
{
# Bekijk of een filter benodigd is
if($filter -ne ''){
ROBOCOPY $source $destination $filter /MOV | Out-Null
}
else
{
ROBOCOPY $source $destination /MOV | Out-Null
}
} catch
{
Write-Logfile -logfile $logfile -level 3 -text "Error executing ROBOCOPY command: $_"
Show-Message -level 3 -text "Error executing ROBOCOPY command: $_"
}
}
}
}
else
{
Write-Logfile -logfile $logfile -level 2 -text "No files found in $source for action $action from $source. Continuing..."
Show-Message -level 2 "No files found in $source for action $action from $source. Continuing..."
}
}
else
{
Write-Logfile -logfile $logfile -level 2 -text "Source folder $source doesn't exist. Continuing..."
Show-Message -level 2 "Source folder $source doesn't exist. Continuing..."
}
}
}
else
{
Write-Logfile -logfile $logfile -level 3 -text "No actions defined! Exiting..."
Show-Message -level 3 "No actions defined! Exiting..."
Exit(0)
}
}
else
{
Write-Logfile -logfile $logfile -level 3 -text "Couldn't find config file! Exiting ..."
Show-Message 3 "Couldn't find config file! Exiting ..."
Exit(0)
}
}
Export-ModuleMember Copy-MoveFiles
复制-移动.psd1
@{
# Script module or binary module file associated with this manifest.
# RootModule = ''
# Version number of this module.
ModuleVersion = '1.0'
# ID used to uniquely identify this module
GUID = 'a3af3a5b-d140-4450-98a1-4602a420e1bb'
# Author of this module
Author = 'Sander Stad'
# Company or vendor of this module
CompanyName = ''
# Copyright statement for this module
Copyright = ''
# Description of the functionality provided by this module
Description = 'Script voor het kopieren en verplaatsen van bestanden'
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '3.0'
# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module
FunctionsToExport = '*'
# Cmdlets to export from this module
CmdletsToExport = '*'
# Variables to export from this module
VariablesToExport = '*'
# Aliases to export from this module
AliasesToExport = '*'
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess
# PrivateData = ''
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
有人知道我做错了什么吗?