Csv Powershell模板

Csv Powershell模板,csv,powershell,Csv,Powershell,我正在为我工作的公司编写一个签名创建者脚本。 它检查登录用户并从CSV文件中获取数据 问题是我需要不同的模板。一个有手机号码,另一个需要徽标,没有手机号码,没有徽标没有手机号码等 因此,我创建了4个HTML模板,它们完成了我希望它们执行的操作,现在唯一的一件事是,当某些内容发生更改时,您需要在所有模板中对其进行更改 是否可以只创建一个模板并用模块或其他东西填充它 因此,您有一个HTML模板,如果用户在CSV文件中有移动或徽标表,则将其添加到该模板中 这是代码,我剥离了html区域: $css =

我正在为我工作的公司编写一个签名创建者脚本。 它检查登录用户并从
CSV
文件中获取数据

问题是我需要不同的模板。一个有手机号码,另一个需要徽标,没有手机号码,没有徽标没有手机号码等

因此,我创建了4个
HTML
模板,它们完成了我希望它们执行的操作,现在唯一的一件事是,当某些内容发生更改时,您需要在所有模板中对其进行更改

是否可以只创建一个模板并用模块或其他东西填充它

因此,您有一个HTML模板,如果用户在CSV文件中有移动或徽标表,则将其添加到该模板中

这是代码,我剥离了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
$css=@”
.signature.signature td{字体系列:Arial,Helvetica,无衬线;字体大小:12px;颜色:#808080;}
.signature hr{背景颜色:透明;边框:无;边框底部:1px实心#dddddddd;}
.签名.免责声明{字体大小:11px;}
.签名A{颜色:#3a7ae2;}
签名A:悬停{文本装饰:无;颜色:#305fab;}
.签名A img{边界:无;}
.signature.separator{padding top:10px;padding right:10px;padding bottom:10px;padding left:10px;margin top:0px;margin right:0px;margin bottom:0px;页边距left:0px;}
.签名p{页边距底部:0.5em;页边距顶部:0;}
.signature td{padding top:10px;padding right:10px;padding bottom:10px;padding left:10px;}
.signature td:first child{padding left:0;}
.签名,nowrap,
.签名跨度,
.签名A{空白:nowrap;}
.signature.MsoNormal{margin top:0;margin bottom:1em;}
@媒体屏幕和屏幕(最大宽度:600px){
.signature tr{显示:块;页边距顶部:10px;页边距右侧:0;页边距底部:10px;页边距左侧:0;}
.signature td{display:block;padding top:0;padding right:0;padding bottom:0;padding left:0;}
.signature.separator{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*=“空白:nowrap;”]{display:block;}
}
"@
$template=@”
{0}
{1}{2}{3}{4}{5}{6}
"@
$template_mobile=@”
{0}
{1}{2}{3}{4}{5}{6}{7}
"@
$template_logo=@”
{0}
{1}{2}{3}{4}{5}{6}{7}
"@
$template_logo_mobile=@”
{0}
{1}{2}{3}{4}{5}{6}{7}{8}
"@
#文件设置
$filename=[Environment]::用户名
$folder\u path=“C:\Users\$filename\AppData\Roaming\Microsoft\handtekenigen”
#CSV文件位置和获取用户名登录到用户的行。
$content=Import CSV-路径“\\shares.e-wise.it\Install\handteking\Uitrolscript\export.CSV”
$user=$content |其中username-eq$Env:username
#是否用户不在CSV中或双停止并关闭
如果(!$user-或$user.Count-gt 1){
写入主机“未找到用户或多个用户”
写入主机“3秒钟后关闭”
开始睡眠-S3
出口
}
#从CSV文件获取所有信息
$name=$user'realname'
$title=$user'jobtitle'
$phone=$user'phone'
$mobile=$user'mobile'
$email=$user'email'
$company=$user.'company'
$hnummer=$user'hnummer'
$afdeling=$user'afdeling'
#检查是否存在地图“Handtekeningen”,否则强制创建它。
新项目-项目类型目录-强制-路径C:\Users\$filename\AppData\Roaming\Microsoft\Handtekeningen | Out Null
if($mobile-like$null-和$afdeling-notlike“Education”){$template-f$css、$name、$title、$phone、$email、$company、$hnummer | Out File-Force$folder_path\$filename.htm}
elseif($mobile-notlike$null-和$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-和$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-和$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”}
#将文件设置为只读(问题是无法覆盖)
设置ItemProperty$folder\u path\$filename.htm-名称IsReadOnly-值$true
#保存并睡眠3秒钟
编写主机“保存签名”
写入主机“3秒钟后关闭”
开始睡眠-S3
CSV构建为:


username、realname、jobtitle、phone、mobile、email、company、hnummer、afdeling
您只需要一个包含两个格式字符串的模板(
{0}
用于css和
{1}
用于正文):


您只需要一个包含两个格式字符串的模板(
{0}
用于
css
{1}
用于正文):


您可以创建一个简单的模板,如下所示:

"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

您可以创建一个简单的模板,如下所示:

"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

请在问题中包括相关代码。如果链接死了,这个问题就会受到影响。请告诉我