首页 > 解决方案 > 第二次运行脚本后填充脚本中的变量

问题描述

有人可以提供一些提示如何用我的脚本解决这个问题吗?脚本描述:它的脚本从 WPF 中创建的 GUI 创建 HTML 签名。问题描述:首先使用 WPF 运行脚本创建具有正确名称的文件夹,复制图像并创建具有正确名称的 HTML,但 HTML 文件只有图像是空白的。在第二次运行脚本后,文件夹、html 文件和图像被正确填充,但包含第一次运行的信息。

我猜它与 $Global 变量有关,因为在我添加一些 $Global 变量后会检测到此问题。

感谢您的任何帮助。

PS1。脚本:

    Add-Type -Name Window -Namespace Console -MemberDefinition
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'

[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)

#-------------------------------------------------------------#
#----Initial Declarations-------------------------------------#
#-------------------------------------------------------------#

Add-Type -AssemblyName PresentationCore, PresentationFramework

$Xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="647" Height="630" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:ListBoxStyle">
 <Window.Resources>
        <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border" Padding="2" SnapsToDevicePixels="true">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="#18509c"/>
                                <Setter Property="Foreground" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
        </Setter>
      </Style>
  </Window.Resources>
<Grid Name="GTX_Podpis" Margin="-2,-1,2,1" MinWidth="1500" MinHeight="1000">


<Image HorizontalAlignment="Left" Height="112" VerticalAlignment="Top" Width="137" Margin="10.84375,11,0,0" Name="Gumotex" Source="\\gtxdc1\Skupiny\Posta podpisy\Husařík_Jakub\logo_gumotex.png"/>

<TextBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" VerticalAlignment="Top" Height="30" Width="120" TextWrapping="Wrap" Margin="185,24,0,0" Name="TitulPred" Tag="TitulPred" Text="" ToolTip="TitulPřed"/>
<TextBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" VerticalAlignment="Top" Height="30" Width="120" TextWrapping="Wrap" Margin="321,24,0,0" Name="TitulZa" Tag="TitulZa" Text="" ToolTip="TitulZa"/>
<TextBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" VerticalAlignment="Top" Height="30" Width="181" TextWrapping="Wrap" Margin="187,76,0,0" Name="Jmeno" ToolTip="Jméno" Text="" Tag="Jmeno"/>
<TextBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" VerticalAlignment="Top" Height="30" Width="181" TextWrapping="Wrap" Margin="187,133,0,0" Name="Prijmeni" Tag="Prijmeni" Text="" ToolTip="Přijmeni"/>
<TextBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" VerticalAlignment="Top" Height="30" Width="181" TextWrapping="Wrap" Margin="189,183,0,0" Name="Funkce" Tag="Funkce" Text="" ToolTip="Funkce"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="181" TextWrapping="Wrap" Margin="189,235,0,0" Name="Email" BorderBrush="#18509c" BorderThickness="2"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="181" TextWrapping="Wrap" Margin="190,290,0,0" Name="Mobil" BorderBrush="#18509c" BorderThickness="2"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="181" TextWrapping="Wrap" Margin="189,349,0,0" Name="PevnaLinka" BorderBrush="#18509c" BorderThickness="2"/>

<ListBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" Height="88" VerticalAlignment="Top" Width="164" Margin="7,75,0,0" Name="Divize" Tag="Divize" ToolTip="Divize" Foreground="#000000" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
>
  <ListBoxItem Content="DIVIZE SERVICE"/>  
  <ListBoxItem Content="DIVIZE COATING"/>  
  <ListBoxItem Content="AUTOMOTIVE DIVISION"/>  
  <ListBoxItem Content="COATING DIVISION"/>
</ListBox>

<ListBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" Height="304" VerticalAlignment="Top" Width="243" Margin="387,75,0,0" Name="Zavod" Tag="Závod" ToolTip="Závod" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
>    
  <ListBoxItem Content="PRÁZDNÉ"/> 
  <ListBoxItem Content="ZÁVOD BŘECLAV"/>  
  <ListBoxItem Content="ZÁVOD TŘEBÍČ"/>  
  <ListBoxItem Content="ZÁVOD JAROMĚŘ"/>  
  <ListBoxItem Content="ZÁVOD MYJAVA"/>
  <ListBoxItem Content="ZÁVOD ROŽNOV"/>
  <ListBoxItem Content="PRODUCTION PLANT BRECLAV"/>
  <ListBoxItem Content="PRODUCTION PLANT TREBIC"/>
  <ListBoxItem Content="PRODUCTION PLANT JAROMER"/>
  <ListBoxItem Content="PRODUCTION PLANT MYJAVA"/>
  <ListBoxItem Content="PRODUCTION PLANT ROZNOV"/>
  <ListBoxItem Content="ENGINEERING DEPARTMENT BRECLAV"/>
  <ListBoxItem Content="ENGINEERING DEPARTMENT TREBIC"/>
  <ListBoxItem Content="ENGINEERING DEPARTMENT JAROMER"/>
  <ListBoxItem Content="ENGINEERING DEPARTMENT MYJAVA"/>
  <ListBoxItem Content="ENGINEERING DEPARTMENT ROZNOV"/>
  
</ListBox>

<ListBox HorizontalAlignment="Left" BorderBrush="#18509c" BorderThickness="2" Height="109" VerticalAlignment="Top" Width="164" Margin="7,184,0,0" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" Name = "Adresa"
>
  <ListBoxItem Content="BŘECLAV"/>  
  <ListBoxItem Content="TŘEBÍČ"/>  
  <ListBoxItem Content="JAROMĚŘ"/>  
  <ListBoxItem Content="MYJAVA"/>
  <ListBoxItem Content="ROŽNOV"/>
</ListBox>


<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Adresa" Margin="9,164,0,0" Name="AdresaLabel"/>


<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Divize" Margin="8,54,0,0" Name="DivizeLabel"/>

<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="TitulPred" Margin="184,5,0,0" Name="TitulPredLabel"/>
<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Jméno" Margin="188,56,0,0" Name="JmenoLabel"/>

<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="TitulZa" Margin="321,5,0,0" Name="TitulZaLabel"/>

<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Přijmení" Margin="187,112,0,0" Name="PrijmeniLabel"/>


<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Funkce" Margin="188,164,0,0" Name="FunkceLabel"/>


<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Závod" Margin="388,55,0,0" Name="ZavodLabel"/>

<Button Content="Vytvoř podpis" HorizontalAlignment="Left" VerticalAlignment="Top" Width="224" Margin="6,527,0,0" Name="VytvorPodpisButton" Background="#18509c" Foreground="#efefef" Height="55" FontSize="15"/>


<Button Content="Zavřít" HorizontalAlignment="Left" VerticalAlignment="Top" Width="224" Margin="403,529,0,0" Height="55" Name="ZavritButton" Background="#18509c" Foreground="#ffffff" FontSize="15"/>





<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Email" Margin="189,214,0,0" Name="EmalLabel"/>


<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Mobil" Margin="190,267,0,0" Name="MobilLabel"/>



<Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Pevná linka" Margin="189,327,0,0" Name="PevnaLinkaLabel"/>

</Grid>

</Window>
"@

#-------------------------------------------------------------#
#----Global variables-----------------------------------#
#-------------------------------------------------------------#
  $Global:ZavodHTML
  $Global:AdresaInHTML

# Břeclav
  $Global:VarAdresaBreclavUlice = "Mládežnická 3062/3a"
  $Global:VarAdresaBreclavMesto = "690 02 Břeclav" 
  $Global:VarAdresaBreclavStat = "Česká republika"
  $Global:VarAdresaBreclav

# Jaroměř  
  $Global:VarAdresaJaromerUlice = "V Lužinách 113, Pražské Předměstí"
  $Global:VarAdresaJaromerMesto = "551 01 Jaroměř" 
  $Global:VarAdresaJaromerStat = "Česká republika"
  $Global:VarAdresaJaromer

# Třebíč
  $Global:arAdresaTrebicUlice = "Žďárského 184"
  $Global:VarAdresaTrebicvMesto = "674 01 Kožichovice" 
  $Global:VarAdresaTrebicvStat = "Česká republika"
  $Global:VarAdresaTrebic

# Myjava
$Global:VarAdresaMyjavaUlice = "Prostredná 1227/9"
$Global:VarAdresaMyjavavMesto = "907 01 Myjava" 
$Global:VarAdresaMyjavaStat = "Slovenská republika"
$Global:VarAdresaMyjava

# Rožnov
$Global:VarAdresaRoznovUlice = "Televizní 2614"
$Global:VarAdresaRoznovMesto = "756 61 Rožnov pod Radhoštěm" 
$Global:VarAdresaRoznovStat = "Česká republika"
$Global:VarAdresaRoznov

# Právní odstavce
#...........................................................................................................................................................................
# Český odstavec
$HTMLTextPravoS = @"
Tato zpráva má pouze informativní charakter. Obsah této zprávy obchodní společnost GUMOTEX, akciová společnost, ani žádnou jinou osobu, která s ní tvoří koncern (dále jen "GUMOTEX"), nezavazuje. GUMOTEX nemá v úmyslu touto zprávou jakoukoliv smlouvu uzavřít, přijmout nabídku nebo uzavření jakékoliv smlouvy potvrdit a zpráva ani nezakládá jeho předsmluvní odpovědnost. Kterákoliv smlouva pro GUMOTEX závazná musí mít výhradně tištěnou písemnou formu, náležitosti dle příslušných právních předpisů a musí být podepsána osobou, která je za GUMOTEX oprávněna jednat nebo GUMOTEX zastupovat. Tato zpráva může obsahovat důvěrné informace. Pokud nejste jejím určeným příjemcem, jakékoliv její zveřejnění, kopírování, rozesílání nebo jiné užití jejího obsahu je přísně zakázáno. Nejste-li určeným příjemcem zprávy, informujte v takovém případě prosím ihned jejího odesílatele a poté zprávu, včetně případných příloh, bezodkladně z Vašeho zařízení smažte.
"@

# Anglický odstavec
$HTMLTextPravoENS = 
@"
This e-mail message is intended only for informational purposes. The content of this message is not binding for neither the company GUMOTEX, akciová společnost, nor any other entity forming the GUMOTEX Group (hereinafter „GUMOTEX“). GUMOTEX does not intend to conclude any agreements, to accept any offers or to confirm the conclusion of any contracts via this message. This message shall not give rise to GUMOTEX precontractual liability. Any agreement binding GUMOTEX shall be in a written form and printed, shall comply with all statutory requirements and shall be signed by a person authorized to act on behalf of or for GUMOTEX. This e-mail message may contain confidential information. If you are not the intended recipient of this message, any disclosure, copying, distribution or use of its contents is strictly prohibited. Please notify the sender immediately and delete the message (including any attachments) from your system.
"@
#...........................................................................................................................................................................

#-------------------------------------------------------------#
#----HTML-----------------------------------------------------#
#-------------------------------------------------------------#

$HTML = @"

<!DOCTYPE html>
<html lang="cs">
<head>
<title>Mail signature</title>
<meta charset="Windows-1250">
<style>
a {
  text-decoration: none;
  color: #000000;
}

a:hover {
  color: #20549A;
}
    
a:active {
  color: #20549A;
    
</style>
    
</head>

<body>
<table>
  <tr>
    <td style="font-family: arial; font-weight: bold; color: #000000; font-size: 16px;">$($TitulPred.Text+$Jmeno.Text+" "+$Prijmeni.Text + $TitulZa.Text)</td>
  </tr>
    <td style="font-family: arial;font-size: 1px;"> </td>
  </tr>
  <tr>
    <td style="font-family: arial; color: #434442; font-size: 14px;">$($Funkce.Text)</td>
 </tr>
$Global:ZavodHTML
  <tr>
    <td style="font-family: arial; color: #20549A; font-weight: bold; font-size: 14px;">$(($Divize.SelectedItem.Content))</td>
 </tr>
  </tr>
    <td style="font-family: arial;font-size: 10px;"> </td>
  </tr>
  <tr>
    <td><img src="logo_gumotex.png" alt="GUMOTEX"></td>
  </tr>
    <td style="font-family: arial;font-size: 8px;"> </td>
  </tr>
 </tr>
</table>
<table>
  <tr>
    <td><img src="icon_envelope_2.png" alt="Email"></td>
    <td style="vertical-align: middle;font-family: arial; color: #000000; font-size: 12px;"><a href="mailto:$($Email.Text)"> $($Email.Text)</a></td>
  </tr>
   <!-- Zde bude promenna pro logiku telefonniho a mobilniho cisla -->
  <tr>
    <td><img src="icon_phone_2.png" alt="$AliasS"></td>
    <td style="vertical-align: middle;font-family: arial; color: #000000; font-size: 12px;"><a href="tel:+$FullTelefonURLS"> +$FullTelefonS | </a><a href="tel:+$MobilURLS">+$MobilS</a></td>
  </tr>
</table> 
$Global:AdresaInHTML
<table>
    <tr>
    <td><a href="https://www.gumotex.cz"><img src="icon_web_2.png" alt="Web"></a></td>
    <td><a href="https://www.facebook.com/gumotex/"><img src="icon_fb_2.png" alt="Facebook"></a></td>
    <td><a href="https://www.linkedin.com/company/gumotex/"><img src="icon_ln_2.png" alt="Linkedin"></a></td>
    <td><a href="https://www.youtube.com/channel/UCHZff_cbSFLqD1kGdhvIrfg"><img src="icon_yt_2.png" alt="YouTube"></a></td>
    </tr>
</table>
<span style="font-family: arial; color: #8f8f8f; font-size: 9px;">
<table>
  </tr>
    <td style="font-family: arial;font-size: 4px;"> </td>
  </tr>
</table>
$TextPravoS
<table>
  </tr>
    <td style="font-family: arial;font-size: 4px;"> </td>
  </tr>
</table>
$TextPravoENS
<table>
  </tr>
    <td style="font-family: arial;font-size: 1px;"> </td>
  </tr>
</table>
</span>
</body>
</html>
"@


#-------------------------------------------------------------#
#----Control Event Handlers-----------------------------------#
#-------------------------------------------------------------#


#Write your code here

$Window = [Windows.Markup.XamlReader]::Parse($Xaml)

[xml]$xml = $Xaml

$xml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name $_.Name -Value $Window.FindName($_.Name) }

#-------------------------------------------------------------#
#----Functions------------------------------------------------#
#-------------------------------------------------------------#

# Formátuje Mobil/Pevnou linku z formátu +420XXXXXXXXX na +420 XXX XXX XXX
<#
Function Format-Contact {
  param (
    [String]$FormatPevnaLinka,
    [String]$FormatMobil
  )
  if (![String]::IsNullOrWhitespace($PevnaLinka.Text)) {
    $HTMLFullTelefonS = $FormatPevnaLinka.Insert(4," ").Insert(8," ").Insert(12," ")
  }
  if (![String]::IsNullOrWhitespace($Mobil.Text)) {
    $HTMLMobilS = $FormatMobil.Insert(4," ").Insert(8," ").Insert(12," ")
  }
}
#>
function CreateFolder_forSignature {
  
  $FolderName = $Jmeno.Text +"_"+ $Prijmeni.Text
  If(!(Test-path -path "\\gtxdc1.gumotex.cz\Skupiny\posta podpisy\$FolderName"))
  {                                                  
    New-Item -ItemType Directory -Force -Path "\\gtxdc1.gumotex.cz\Skupiny\Posta podpisy\" -Name $FolderName
    }
  else {
                                                  
      [System.Windows.MessageBox]::Show("Složka $FolderName na síťovém disku:\\gtxdc1\skupiny\posta podpisy\ již existuje, pokud chcete vytvořit znovu tuto složku prosím smažte/přejmenujte/přesuňte stávající složku.")
  }
}
function  CopyImages_ToNewFolder {
  $FolderName = $Jmeno.Text +"_"+ $Prijmeni.Text
  Copy-Item -Path "\\gtxdc1\skupiny\posta podpisy\Obrazky\*" -Destination "\\gtxdc1\skupiny\posta podpisy\$FolderName" -Recurse 
}

# Functions
# Funkce pro logiku Zavodu
function CreateZavod_toHTML {
 <#if ($Zavod.SelectedItem.Content -eq "PRÁZDNÉ")
  {
    $Global:ZavodHTML = $null
 }
 else {#>
   $Global:ZavodHTML =   @"
   <tr>
   <td style="font-family: arial; color: #20549A; font-weight: bold; font-size: 14px;">$($Zavod.SelectedItem.Content)</td>
   </tr>
"@
 
}
# Funkce pro logiku Adresy
function CreateAddress_ToHTML {
  $VarUlice
  $VarMesto
  $VarStat
  switch ($Adresa.SelectedItem.Content) {
    "BŘECLAV" { 
      $VarUlice = $Global:VarAdresaBreclavUlice
      $VarMesto = $Global:VarAdresaBreclavMesto
      $VarStat = $Global:VarAdresaBreclavStat
    }
    "TŘEBÍČ" { 
      $VarUlice = $Global:VarAdresaTrebicUlice
      $VarMesto = $Global:VarAdresaTrebicvMesto
      $VarStat = $Global:VarAdresaTrebicvStat
     }
    "JAROMĚŘ" { 
      $VarUlice = $Global:VarAdresaJaromerUlice
      $VarMesto = $Global:VarAdresaJaromerMesto
      $VarStat = $Global:VarAdresaJaromerStat
     }
    "MYJAVA" { 
      $VarUlice = $Global:VarAdresaMyjavaUlice
      $VarMesto = $Global:VarAdresaMyjavavMesto
      $VarStat = $Global:VarAdresaMyjavaStat
     }
    "ROŽNOV" { 
      $VarUlice = $Global:VarAdresaRoznovUlice
      $VarMesto = $Global:VarAdresaRoznovMesto
      $VarStat = $Global:VarAdresaRoznovStaT
     }
    }
     $Global:AdresaInHTML =  @"
<table>
  </tr>
    <td style="font-family: arial;font-size: 4px;"> </td>
  </tr>
  <tr>
    <td style="font-family: arial; color: #717270; font-size: 11px;">$VarUlice</td>
  </tr>
  <tr>
    <td style="font-family: arial; color: #717270; font-size: 11px;">$VarMesto</td>
  </tr>
 <tr>
    <td style="font-family: arial; color: #717270; font-size: 11px;">$VarStat</td>
  </tr>
  </tr>
    <td style="font-family: arial;font-size: 10px;"> </td>
  </tr>
</table>
"@
}
function CreateHTMLSignature { 
  $FolderName = $Jmeno.Text +"_"+ $Prijmeni.Text
  $FileName = $Jmeno.Text +"_"+ $Prijmeni.Text + ".html"
  New-Item -ItemType File -Path "\\gtxdc1\Skupiny\Posta podpisy\$FolderName\" -Name $FileName
  Set-Content -Path "\\gtxdc1\Skupiny\Posta podpisy\$FolderName\$FileName" -Value $HTML
}

$ZavritButton.Add_Click({
    $Window.Close()
})
$VytvorPodpisButton.Add_Click({  
CreateFolder_forSignature
CopyImages_ToNewFolder
CreateAddress_ToHTML
CreateZavod_toHTML
CreateHTMLSignature
$Window.Close()
})

#endregion

#-------------------------------------------------------------#
#----Script Execution-----------------------------------------#
#-------------------------------------------------------------#



    #Format-Contact -FormatPevnaLinka $PevnaLinka.Text -FormatMobil $Mobil.Text

$Window.ShowDialog()

标签: wpfpowershell

解决方案


解决方案成立:在脚本的开头我放了这行代码:Remove-Variable * -ErrorAction SilentlyContinue解决了这个问题


推荐阅读