我正在开发一个生成Outlook html签名的powershell脚本。 它现在有效,但您需要手动填写所有模板字(姓名电子邮件等)
我想要的是,当您运行powershell脚本时,它会检查登录用户并从CSV文件中获取该用户的信息并将其填入html模板。
这样做的最佳方法是什么?
我现在得到的是:
$html_template = @"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
etc etc etc
"@
# Simulate how ever you are querying for data.
$desktop_path = "$env:userprofile\Desktop\"
$filename = [Environment]::UserName
# Does nothing only import
$content = Import-CSV -Path D:\export.csv
# Input fields
$name= Read-Host -Prompt 'Input name (eg. Tim Tom)'
$title= Read-Host -Prompt 'Input the job title (eg. Office Manager)'
$phone= Read-Host -Prompt 'Input the last 3 telephone numbers (eg. 111)'
$email= Read-Host -Prompt 'Input email adress (eg. name@domain.nl)'
# Check if map Handtekeningen exist else force create it.
New-Item -ItemType Directory -Force -Path C:\Users\$filename\AppData\Roaming\Microsoft\Handtekeningen | Out-Null
# Create html file
$html_template -f $name,$title,$phone,$email,$filename | Out-File -Force $desktop_path\$filename.html
# Set file to Readonly (problem is it cannot be overwriten)
Set-ItemProperty $desktop_path\$filename.html -name IsReadOnly -value $true
# Write
Write-Host "Saving signature."
Write-Host "Closing in 5 seconds."
# Sleep for 5 seconds
Start-Sleep -s 5
CSV:
username;Full Name;Job Title;Telephone;Email
答案 0 :(得分:1)
如果(在您的评论之后)我理解正确,您只需要确定CSV文件中要使用的行。在这种情况下,您可以从环境变量USERNAME
中读取用户名,并简单地过滤:
$content = Import-CSV -Path D:\export.csv
$user = $content | where username -eq $Env:USERNAME
if (!$user -or $user.Count -gt 1) {
throw 'No user, or multiple users found'
}
# Input fields
$name= $user.'Full Name'
$title= $user.'Job Title'
$phone= $user.Telephone
$email= $user.'Email'