我正在爲我工作的公司製作簽名創建者腳本。 它檢查登錄的用戶並從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
請在問題中包含相關代碼。如果鏈接死亡,這個問題將會受到影響。 – Matt