2016-06-22 35 views
1

我正在爲我工​​作的公司製作簽名創建者腳本。 它檢查登錄的用戶並從CSV文件獲取數據。Powershell模板

問題是我需要不同的模板。一個手機號碼,另外一個需要一個標誌,沒有手機號碼,沒有標誌沒有手機號碼等。

所以我創建了4 HTML模板,做我想讓他們做的事情,現在唯一的事情是,當你需要改變它的一些變化所有的模板

是否有可能只創建一個模板並填充模塊或其他東西?

因此,如果用戶在CSV文件中包含移動或徽標表,那麼您只有一個HTML模板並添加了移動或徽標表。

下面是代碼,我扯下了HTML方面:

$css = @" 
.signature, .signature td {font-family:Arial, Helvetica, sans serif;font-size:12px;color:#808080;} 
.signature hr {background-color:transparent;border:none;border-bottom:1px solid #dddddd;} 
.signature .disclaimer {font-size:11px;} 
.signature A {color:#3a7ae2;} 
.signature A:hover {text-decoration:none;color:#305fab;} 
.signature A img {border:none;} 
.signature .seperator {padding-top: 10px;padding-right: 10px;padding-bottom: 10px;padding-left: 10px;margin-top: 0px;margin-right: 0px;margin-bottom: 0px;margin-left: 0px;} 
.signature p {margin-bottom:0.5em;margin-top:0;} 

.signature td {padding-top: 10px;padding-right: 10px;padding-bottom: 10px;padding-left: 10px;} 
.signature td:first-child {padding-left: 0;} 

.signature .nowrap, 
.signature span, 
.signature A {white-space:nowrap;} 
.signature .MsoNormal {margin-top:0;margin-bottom: 1em;} 

@media screen and (max-width:600px) { 
    .signature tr {display:block;margin-top: 10px;margin-right: 0;margin-bottom: 10px;margin-left: 0;} 
    .signature td {display:block;padding-top: 0;padding-right: 0;padding-bottom: 0;padding-left: 0;} 
    .signature .seperator {display:none;padding-top: 0;padding-right: 0;padding-bottom: 0;padding-left: 0;margin-top: 0;margin-right: 0;margin-bottom: 0;margin-left: 0;} 
    .signature [style*="white-space:nowrap;"] {display:block;} 
} 
"@ 
$template = @" 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<style type='text/css'> 
{0} 
</style> 

{1}{2}{3}{4}{5}{6} 
"@ 
$template_mobile = @" 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<style type='text/css'> 
{0} 
</style> 

{1}{2}{3}{4}{5}{6}{7} 
"@ 
$template_logo = @" 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<style type='text/css'> 
{0} 
</style> 

{1}{2}{3}{4}{5}{6}{7} 
"@ 
$template_logo_mobile = @" 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<style type='text/css'> 
{0} 
</style> 
{1}{2}{3}{4}{5}{6}{7}{8} 
"@ 

# File settings 
$filename = [Environment]::UserName 
$folder_path = "C:\Users\$filename\AppData\Roaming\Microsoft\Handtekeningen" 

# CSV file location and get row where username is logged in user. 
$content = Import-CSV -Path "\\shares.e-wise.it\Install\handtekening\Uitrolscript\export.csv" 
$user = $content | where username -eq $Env:USERNAME 

# Is user is not in CSV or double stop and close 
if (!$user -or $user.Count -gt 1) { 
    Write-Host 'No user, or multiple users found' 
    Write-Host "Closing in 3 seconds." 
    Start-Sleep -s 3 
    exit 
} 

# Get all info from CSV file 
$name= $user.'realname' 
$title= $user.'jobtitle' 
$phone= $user.'phone' 
$mobile= $user.'mobile' 
$email= $user.'email' 
$company= $user.'company' 
$hnummer= $user.'hnummer' 
$afdeling= $user.'afdeling' 

# Check if map "Handtekeningen" exist else force create it. 
New-Item -ItemType Directory -Force -Path C:\Users\$filename\AppData\Roaming\Microsoft\Handtekeningen | Out-Null 

if ($mobile -like $null -And $afdeling -notlike "Education") {$template -f $css,$name,$title,$phone,$email,$company,$hnummer | Out-File -Force $folder_path\$filename.htm} 
elseif ($mobile -notlike $null -And $afdeling -notlike "Education") {$template_mobile -f $css,$name,$title,$phone,$mobile,$email,$company,$hnummer | Out-File -Force $folder_path\$filename.htm} 
elseif ($mobile -like $null -And $afdeling -like "Education") {$template_logo -f $css,$name,$title,$phone,$email,$company,$hnummer,$afdeling | Out-File -Force $folder_path\$filename.htm} 
elseif ($mobile -notlike $null -And $afdeling -like "Education") {$template_logo_mobile -f $css,$name,$title,$phone,$mobile,$email,$company,$hnummer,$afdeling | Out-File -Force $folder_path\$filename.htm} 
else {Write-Host "Nothing to do fix in export.csv"} 

# Set file to Readonly (problem is it cannot be overwriten) 
Set-ItemProperty $folder_path\$filename.htm -name IsReadOnly -value $true 

# Saving and sleep for 3 seconds 
Write-Host "Saving signature." 
Write-Host "Closing in 3 seconds." 
Start-Sleep -s 3 

的CSV建立是:

username,realname,jobtitle,phone,mobile,email,company,hnummer,afdeling

+0

請在問題中包含相關代碼。如果鏈接死亡,這個問題將會受到影響。 – Matt

回答

0

所有你需要的是一個包含兩個一個模板格式字符串{0}css{1}):

$template = @" 
<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<style type='text/css'> 
{0} 
</style> 

{1} 
"@ 

現在您將兩個參數傳遞給格式字符串。第一個是CSS,第二個是您使用-join合併的主體內容列表。例如:

if ($mobile -like $null -And $afdeling -notlike "Education") { 
    $template -f $css, ($name,$title,$phone,$email,$company,$hnummer -join ' ') | Out-File -Force $folder_path\$filename.htm 
} 
0

你可以像這樣創建一個簡單的模板:

"testing {0} {2}" -f 1, 2 

問題是,當模板越來越大,存在很多變數它可能會比較混亂獲取變量正確的順序。更好的解決方案是在你的模板中有命名變量。

如果你使用像這樣的功能:

function Invoke-Template 
{ 
    param([string]$template, [hashtable]$data) 
    [regex]::Matches($x, '\{\{\w+\}\}').Groups | % { 
     New-Object psobject -Property @{ 
      name = [regex]::Match($_.value, '\w').Value 
      regex = [regex]::Escape($_) 
     } 
    } | % {$template = $template -replace $_.regex, $data[$_.name]} 
    return $template 
} 

然後,您可以在{{}}格式

$x = @" 
Test {{name1}} 
Test {{name2}} 
Test {{name1}} 
"@ 

接下來創建一個模板,命名佔位符創建使用相同的數據散列命名palceholders

$data = @{name1 = '1'; name2 = '2'} 

然後調用模板:

Invoke-Template -template $x -data $data 

您可以輕鬆修改數據並使用不同的數據再次調用模板。

$data['name1'] = 33 
Invoke-Template -template $x -data $data